blob: a19d3db494165bae9638e453f092e4e5660d0632 [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>
26
27#include "wayland-server.h"
28#include "compositor.h"
29
30struct wlsc_move_grab {
31 struct wl_grab grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050032 struct wlsc_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050033 int32_t dx, dy;
34};
35
36static void
37move_grab_motion(struct wl_grab *grab,
38 uint32_t time, int32_t x, int32_t y)
39{
40 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050041 struct wlsc_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050042
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -050043 wlsc_surface_damage(es);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050044 es->x = x + move->dx;
45 es->y = y + move->dy;
46 wlsc_surface_update_matrix(es);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -050047 wlsc_surface_damage(es);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050048}
49
50static void
51move_grab_button(struct wl_grab *grab,
52 uint32_t time, int32_t button, int32_t state)
53{
54}
55
56static void
57move_grab_end(struct wl_grab *grab, uint32_t time)
58{
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050059 struct wlsc_surface *es;
60 struct wl_input_device *device = grab->input_device;
61 int32_t sx, sy;
62
63 es = pick_surface(grab->input_device, &sx, &sy);
64 wl_input_device_set_pointer_focus(device,
65 &es->surface, time,
66 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050067 free(grab);
68}
69
70static const struct wl_grab_interface move_grab_interface = {
71 move_grab_motion,
72 move_grab_button,
73 move_grab_end
74};
75
76void
77shell_move(struct wl_client *client, struct wl_shell *shell,
78 struct wl_surface *surface,
79 struct wl_input_device *device, uint32_t time)
80{
81 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
82 struct wlsc_surface *es = (struct wlsc_surface *) surface;
83 struct wlsc_move_grab *move;
84
Kristian Høgsberg0ce24572011-01-28 15:18:33 -050085 /* FIXME: Reject if fullscreen */
86
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050087 move = malloc(sizeof *move);
88 if (!move) {
89 wl_client_post_no_memory(client);
90 return;
91 }
92
93 move->grab.interface = &move_grab_interface;
94 move->dx = es->x - wd->input_device.grab_x;
95 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050096 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050097
98 if (wl_input_device_update_grab(&wd->input_device,
99 &move->grab, surface, time) < 0)
100 return;
101
102 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500103 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500104}
105
106struct wlsc_resize_grab {
107 struct wl_grab grab;
108 uint32_t edges;
109 int32_t dx, dy, width, height;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500110 struct wlsc_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500111};
112
113static void
114resize_grab_motion(struct wl_grab *grab,
115 uint32_t time, int32_t x, int32_t y)
116{
117 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
118 struct wl_input_device *device = grab->input_device;
119 struct wlsc_compositor *ec =
120 (struct wlsc_compositor *) device->compositor;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500121 struct wl_surface *surface = &resize->surface->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500122 int32_t width, height;
123
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500124 if (resize->edges & WL_SHELL_RESIZE_LEFT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500125 width = device->grab_x - x + resize->width;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500126 } else if (resize->edges & WL_SHELL_RESIZE_RIGHT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500127 width = x - device->grab_x + resize->width;
128 } else {
129 width = resize->width;
130 }
131
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500132 if (resize->edges & WL_SHELL_RESIZE_TOP) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500133 height = device->grab_y - y + resize->height;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500134 } else if (resize->edges & WL_SHELL_RESIZE_BOTTOM) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500135 height = y - device->grab_y + resize->height;
136 } else {
137 height = resize->height;
138 }
139
140 wl_client_post_event(surface->client, &ec->shell.object,
141 WL_SHELL_CONFIGURE, time, resize->edges,
142 surface, width, height);
143}
144
145static void
146resize_grab_button(struct wl_grab *grab,
147 uint32_t time, int32_t button, int32_t state)
148{
149}
150
151static void
152resize_grab_end(struct wl_grab *grab, uint32_t time)
153{
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500154 struct wlsc_surface *es;
155 struct wl_input_device *device = grab->input_device;
156 int32_t sx, sy;
157
158 es = pick_surface(grab->input_device, &sx, &sy);
159 wl_input_device_set_pointer_focus(device,
160 &es->surface, time,
161 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500162 free(grab);
163}
164
165static const struct wl_grab_interface resize_grab_interface = {
166 resize_grab_motion,
167 resize_grab_button,
168 resize_grab_end
169};
170
171void
172shell_resize(struct wl_client *client, struct wl_shell *shell,
173 struct wl_surface *surface,
174 struct wl_input_device *device, uint32_t time, uint32_t edges)
175{
176 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
177 struct wlsc_resize_grab *resize;
178 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
179 struct wlsc_surface *es = (struct wlsc_surface *) surface;
180
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500181 /* FIXME: Reject if fullscreen */
182
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500183 resize = malloc(sizeof *resize);
184 if (!resize) {
185 wl_client_post_no_memory(client);
186 return;
187 }
188
189 resize->grab.interface = &resize_grab_interface;
190 resize->edges = edges;
191 resize->dx = es->x - wd->input_device.grab_x;
192 resize->dy = es->y - wd->input_device.grab_y;
193 resize->width = es->width;
194 resize->height = es->height;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500195 resize->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500196
197 if (edges == 0 || edges > 15 ||
198 (edges & 3) == 3 || (edges & 12) == 12)
199 return;
200
201 switch (edges) {
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500202 case WL_SHELL_RESIZE_TOP:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500203 pointer = WLSC_POINTER_TOP;
204 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500205 case WL_SHELL_RESIZE_BOTTOM:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500206 pointer = WLSC_POINTER_BOTTOM;
207 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500208 case WL_SHELL_RESIZE_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500209 pointer = WLSC_POINTER_LEFT;
210 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500211 case WL_SHELL_RESIZE_TOP_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500212 pointer = WLSC_POINTER_TOP_LEFT;
213 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500214 case WL_SHELL_RESIZE_BOTTOM_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500215 pointer = WLSC_POINTER_BOTTOM_LEFT;
216 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500217 case WL_SHELL_RESIZE_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500218 pointer = WLSC_POINTER_RIGHT;
219 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500220 case WL_SHELL_RESIZE_TOP_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500221 pointer = WLSC_POINTER_TOP_RIGHT;
222 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500223 case WL_SHELL_RESIZE_BOTTOM_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500224 pointer = WLSC_POINTER_BOTTOM_RIGHT;
225 break;
226 }
227
228 if (wl_input_device_update_grab(&wd->input_device,
229 &resize->grab, surface, time) < 0)
230 return;
231
232 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500233 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500234}
235
236static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500237destroy_drag(struct wl_resource *resource, struct wl_client *client)
238{
239 struct wl_drag *drag =
240 container_of(resource, struct wl_drag, resource);
241
242 wl_list_remove(&drag->drag_focus_listener.link);
243 if (drag->grab.input_device)
244 wl_input_device_end_grab(drag->grab.input_device, get_time());
245
246 free(drag);
247}
248
249
250static void
251wl_drag_set_pointer_focus(struct wl_drag *drag,
252 struct wl_surface *surface, uint32_t time,
253 int32_t x, int32_t y, int32_t sx, int32_t sy)
254{
255 char **p, **end;
256
257 if (drag->drag_focus == surface)
258 return;
259
260 if (drag->drag_focus &&
261 (!surface || drag->drag_focus->client != surface->client))
262 wl_client_post_event(drag->drag_focus->client,
263 &drag->drag_offer.object,
264 WL_DRAG_OFFER_POINTER_FOCUS,
265 time, NULL, 0, 0, 0, 0);
266
267 if (surface &&
268 (!drag->drag_focus ||
269 drag->drag_focus->client != surface->client)) {
270 wl_client_post_global(surface->client,
271 &drag->drag_offer.object);
272
273 end = drag->types.data + drag->types.size;
274 for (p = drag->types.data; p < end; p++)
275 wl_client_post_event(surface->client,
276 &drag->drag_offer.object,
277 WL_DRAG_OFFER_OFFER, *p);
278 }
279
280 if (surface) {
281 wl_client_post_event(surface->client,
282 &drag->drag_offer.object,
283 WL_DRAG_OFFER_POINTER_FOCUS,
284 time, surface,
285 x, y, sx, sy);
286
287 }
288
289 drag->drag_focus = surface;
290 drag->pointer_focus_time = time;
291 drag->target = NULL;
292
293 wl_list_remove(&drag->drag_focus_listener.link);
294 if (surface)
295 wl_list_insert(surface->destroy_listener_list.prev,
296 &drag->drag_focus_listener.link);
297}
298
299static void
300drag_offer_accept(struct wl_client *client,
301 struct wl_drag_offer *offer, uint32_t time, const char *type)
302{
303 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
304 char **p, **end;
305
306 /* If the client responds to drag pointer_focus or motion
307 * events after the pointer has left the surface, we just
308 * discard the accept requests. The drag source just won't
309 * get the corresponding 'target' events and eventually the
310 * next surface/root will start sending events. */
311 if (time < drag->pointer_focus_time)
312 return;
313
314 drag->target = client;
315 drag->type = NULL;
316 end = drag->types.data + drag->types.size;
317 for (p = drag->types.data; p < end; p++)
318 if (type && strcmp(*p, type) == 0)
319 drag->type = *p;
320
321 wl_client_post_event(drag->source->client, &drag->resource.object,
322 WL_DRAG_TARGET, drag->type);
323}
324
325static void
326drag_offer_receive(struct wl_client *client,
327 struct wl_drag_offer *offer, int fd)
328{
329 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
330
331 wl_client_post_event(drag->source->client, &drag->resource.object,
332 WL_DRAG_FINISH, fd);
333 close(fd);
334}
335
336static void
337drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
338{
339 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
340
341 wl_client_post_event(drag->source->client, &drag->resource.object,
342 WL_DRAG_REJECT);
343}
344
345static const struct wl_drag_offer_interface drag_offer_interface = {
346 drag_offer_accept,
347 drag_offer_receive,
348 drag_offer_reject
349};
350
351static void
352drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
353{
354 char **p;
355
356 p = wl_array_add(&drag->types, sizeof *p);
357 if (p)
358 *p = strdup(type);
359 if (!p || !*p)
360 wl_client_post_no_memory(client);
361}
362
363static void
364drag_grab_motion(struct wl_grab *grab,
365 uint32_t time, int32_t x, int32_t y)
366{
367 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
368 struct wlsc_surface *es;
369 int32_t sx, sy;
370
371 es = pick_surface(grab->input_device, &sx, &sy);
372 wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy);
373 if (es)
374 wl_client_post_event(es->surface.client,
375 &drag->drag_offer.object,
376 WL_DRAG_OFFER_MOTION,
377 time, x, y, sx, sy);
378}
379
380static void
381drag_grab_button(struct wl_grab *grab,
382 uint32_t time, int32_t button, int32_t state)
383{
384}
385
386static void
387drag_grab_end(struct wl_grab *grab, uint32_t time)
388{
389 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500390 struct wlsc_surface *es;
391 struct wl_input_device *device = grab->input_device;
392 int32_t sx, sy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500393
394 if (drag->target)
395 wl_client_post_event(drag->target,
396 &drag->drag_offer.object,
397 WL_DRAG_OFFER_DROP);
398
399 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500400
401 es = pick_surface(grab->input_device, &sx, &sy);
402 wl_input_device_set_pointer_focus(device,
403 &es->surface, time,
404 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500405}
406
407static const struct wl_grab_interface drag_grab_interface = {
408 drag_grab_motion,
409 drag_grab_button,
410 drag_grab_end
411};
412
413static void
414drag_activate(struct wl_client *client,
415 struct wl_drag *drag,
416 struct wl_surface *surface,
417 struct wl_input_device *device, uint32_t time)
418{
419 struct wl_display *display = wl_client_get_display (client);
420 struct wlsc_surface *target;
421 int32_t sx, sy;
422
423 if (wl_input_device_update_grab(device,
424 &drag->grab, surface, time) < 0)
425 return;
426
427 drag->grab.interface = &drag_grab_interface;
428
429 drag->source = surface;
430
431 drag->drag_offer.object.interface = &wl_drag_offer_interface;
432 drag->drag_offer.object.implementation =
433 (void (**)(void)) &drag_offer_interface;
434
435 wl_display_add_object(display, &drag->drag_offer.object);
436
437 target = pick_surface(device, &sx, &sy);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500438 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500439 wl_drag_set_pointer_focus(drag, &target->surface, time,
440 device->x, device->y, sx, sy);
441}
442
443static void
444drag_destroy(struct wl_client *client, struct wl_drag *drag)
445{
446 wl_resource_destroy(&drag->resource, client);
447}
448
449static const struct wl_drag_interface drag_interface = {
450 drag_offer,
451 drag_activate,
452 drag_destroy,
453};
454
455static void
456drag_handle_surface_destroy(struct wl_listener *listener,
457 struct wl_surface *surface, uint32_t time)
458{
459 struct wl_drag *drag =
460 container_of(listener, struct wl_drag, drag_focus_listener);
461
462 if (drag->drag_focus == surface)
463 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
464}
465
466static void
467shell_create_drag(struct wl_client *client,
468 struct wl_shell *shell, uint32_t id)
469{
470 struct wl_drag *drag;
471
472 drag = malloc(sizeof *drag);
473 if (drag == NULL) {
474 wl_client_post_no_memory(client);
475 return;
476 }
477
478 memset(drag, 0, sizeof *drag);
479 drag->resource.object.id = id;
480 drag->resource.object.interface = &wl_drag_interface;
481 drag->resource.object.implementation =
482 (void (**)(void)) &drag_interface;
483
484 drag->resource.destroy = destroy_drag;
485
486 drag->drag_focus_listener.func = drag_handle_surface_destroy;
487 wl_list_init(&drag->drag_focus_listener.link);
488
489 wl_client_add_resource(client, &drag->resource);
490}
491
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500492void
493wlsc_selection_set_focus(struct wl_selection *selection,
494 struct wl_surface *surface, uint32_t time)
495{
496 char **p, **end;
497
498 if (selection->selection_focus == surface)
499 return;
500
501 if (selection->selection_focus != NULL)
502 wl_client_post_event(selection->selection_focus->client,
503 &selection->selection_offer.object,
504 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
505 NULL);
506
507 if (surface) {
508 wl_client_post_global(surface->client,
509 &selection->selection_offer.object);
510
511 end = selection->types.data + selection->types.size;
512 for (p = selection->types.data; p < end; p++)
513 wl_client_post_event(surface->client,
514 &selection->selection_offer.object,
515 WL_SELECTION_OFFER_OFFER, *p);
516
517 wl_list_remove(&selection->selection_focus_listener.link);
518 wl_list_insert(surface->destroy_listener_list.prev,
519 &selection->selection_focus_listener.link);
520
521 wl_client_post_event(surface->client,
522 &selection->selection_offer.object,
523 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
524 selection->input_device);
525 }
526
527 selection->selection_focus = surface;
528
529 wl_list_remove(&selection->selection_focus_listener.link);
530 if (surface)
531 wl_list_insert(surface->destroy_listener_list.prev,
532 &selection->selection_focus_listener.link);
533}
534
535static void
536selection_offer_receive(struct wl_client *client,
537 struct wl_selection_offer *offer,
538 const char *mime_type, int fd)
539{
540 struct wl_selection *selection =
541 container_of(offer, struct wl_selection, selection_offer);
542
543 wl_client_post_event(selection->client,
544 &selection->resource.object,
545 WL_SELECTION_SEND, mime_type, fd);
546 close(fd);
547}
548
549static const struct wl_selection_offer_interface selection_offer_interface = {
550 selection_offer_receive
551};
552
553static void
554selection_offer(struct wl_client *client,
555 struct wl_selection *selection, const char *type)
556{
557 char **p;
558
559 p = wl_array_add(&selection->types, sizeof *p);
560 if (p)
561 *p = strdup(type);
562 if (!p || !*p)
563 wl_client_post_no_memory(client);
564}
565
566static void
567selection_activate(struct wl_client *client,
568 struct wl_selection *selection,
569 struct wl_input_device *device, uint32_t time)
570{
571 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
572 struct wl_display *display = wl_client_get_display (client);
573
574 selection->input_device = device;
575
576 selection->selection_offer.object.interface =
577 &wl_selection_offer_interface;
578 selection->selection_offer.object.implementation =
579 (void (**)(void)) &selection_offer_interface;
580
581 wl_display_add_object(display, &selection->selection_offer.object);
582
583 if (wd->selection) {
584 wl_client_post_event(wd->selection->client,
585 &wd->selection->resource.object,
586 WL_SELECTION_CANCELLED);
587 }
588 wd->selection = selection;
589
590 wlsc_selection_set_focus(selection, device->keyboard_focus, time);
591}
592
593static void
594selection_destroy(struct wl_client *client, struct wl_selection *selection)
595{
596 wl_resource_destroy(&selection->resource, client);
597}
598
599static const struct wl_selection_interface selection_interface = {
600 selection_offer,
601 selection_activate,
602 selection_destroy
603};
604
605static void
606destroy_selection(struct wl_resource *resource, struct wl_client *client)
607{
608 struct wl_selection *selection =
609 container_of(resource, struct wl_selection, resource);
610 struct wlsc_input_device *wd =
611 (struct wlsc_input_device *) selection->input_device;
612
613 if (wd && wd->selection == selection) {
614 wd->selection = NULL;
615 wlsc_selection_set_focus(selection, NULL, get_time());
616 }
617
618 wl_list_remove(&selection->selection_focus_listener.link);
619 free(selection);
620}
621
622static void
623selection_handle_surface_destroy(struct wl_listener *listener,
624 struct wl_surface *surface, uint32_t time)
625{
626}
627
628static void
629shell_create_selection(struct wl_client *client,
630 struct wl_shell *shell, uint32_t id)
631{
632 struct wl_selection *selection;
633
634 selection = malloc(sizeof *selection);
635 if (selection == NULL) {
636 wl_client_post_no_memory(client);
637 return;
638 }
639
640 memset(selection, 0, sizeof *selection);
641 selection->resource.object.id = id;
642 selection->resource.object.interface = &wl_selection_interface;
643 selection->resource.object.implementation =
644 (void (**)(void)) &selection_interface;
645
646 selection->client = client;
647 selection->resource.destroy = destroy_selection;
648 selection->selection_focus = NULL;
649
650 selection->selection_focus_listener.func =
651 selection_handle_surface_destroy;
652 wl_list_init(&selection->selection_focus_listener.link);
653
654 wl_client_add_resource(client, &selection->resource);
655}
656
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500657const static struct wl_shell_interface shell_interface = {
658 shell_move,
659 shell_resize,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500660 shell_create_drag,
661 shell_create_selection
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500662};
663
664int
665wlsc_shell_init(struct wlsc_compositor *ec)
666{
667 struct wl_shell *shell = &ec->shell;
668
669 shell->object.interface = &wl_shell_interface;
670 shell->object.implementation = (void (**)(void)) &shell_interface;
671 wl_display_add_object(ec->wl_display, &shell->object);
672 if (wl_display_add_global(ec->wl_display, &shell->object, NULL))
673 return -1;
674
675 return 0;
676}