blob: d3859aeaa86f3303fc7c36b4bfd4972394fca5c0 [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øgsberg4cca3492011-01-18 07:53:49 -050053 wlsc_surface_update_matrix(es);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -050054 wlsc_surface_damage(es);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050055}
56
57static void
58move_grab_button(struct wl_grab *grab,
59 uint32_t time, int32_t button, int32_t state)
60{
61}
62
63static void
64move_grab_end(struct wl_grab *grab, uint32_t time)
65{
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050066 struct wlsc_surface *es;
67 struct wl_input_device *device = grab->input_device;
68 int32_t sx, sy;
69
70 es = pick_surface(grab->input_device, &sx, &sy);
71 wl_input_device_set_pointer_focus(device,
72 &es->surface, time,
73 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050074 free(grab);
75}
76
77static const struct wl_grab_interface move_grab_interface = {
78 move_grab_motion,
79 move_grab_button,
80 move_grab_end
81};
82
Kristian Høgsberg07937562011-04-12 17:25:42 -040083static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050084shell_move(struct wl_client *client, struct wl_shell *shell,
85 struct wl_surface *surface,
86 struct wl_input_device *device, uint32_t time)
87{
88 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
89 struct wlsc_surface *es = (struct wlsc_surface *) surface;
90 struct wlsc_move_grab *move;
91
Kristian Høgsberg0ce24572011-01-28 15:18:33 -050092 /* FIXME: Reject if fullscreen */
93
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050094 move = malloc(sizeof *move);
95 if (!move) {
96 wl_client_post_no_memory(client);
97 return;
98 }
99
100 move->grab.interface = &move_grab_interface;
101 move->dx = es->x - wd->input_device.grab_x;
102 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500103 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500104
105 if (wl_input_device_update_grab(&wd->input_device,
106 &move->grab, surface, time) < 0)
107 return;
108
109 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500110 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500111}
112
113struct wlsc_resize_grab {
114 struct wl_grab grab;
115 uint32_t edges;
116 int32_t dx, dy, width, height;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500117 struct wlsc_surface *surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400118 struct wl_shell *shell;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500119};
120
121static void
122resize_grab_motion(struct wl_grab *grab,
123 uint32_t time, int32_t x, int32_t y)
124{
125 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
126 struct wl_input_device *device = grab->input_device;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500127 struct wl_surface *surface = &resize->surface->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500128 int32_t width, height;
129
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500130 if (resize->edges & WL_SHELL_RESIZE_LEFT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500131 width = device->grab_x - x + resize->width;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500132 } else if (resize->edges & WL_SHELL_RESIZE_RIGHT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500133 width = x - device->grab_x + resize->width;
134 } else {
135 width = resize->width;
136 }
137
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500138 if (resize->edges & WL_SHELL_RESIZE_TOP) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500139 height = device->grab_y - y + resize->height;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500140 } else if (resize->edges & WL_SHELL_RESIZE_BOTTOM) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500141 height = y - device->grab_y + resize->height;
142 } else {
143 height = resize->height;
144 }
145
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400146 wl_client_post_event(surface->client, &resize->shell->object,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500147 WL_SHELL_CONFIGURE, time, resize->edges,
148 surface, width, height);
149}
150
151static void
152resize_grab_button(struct wl_grab *grab,
153 uint32_t time, int32_t button, int32_t state)
154{
155}
156
157static void
158resize_grab_end(struct wl_grab *grab, uint32_t time)
159{
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500160 struct wlsc_surface *es;
161 struct wl_input_device *device = grab->input_device;
162 int32_t sx, sy;
163
164 es = pick_surface(grab->input_device, &sx, &sy);
165 wl_input_device_set_pointer_focus(device,
166 &es->surface, time,
167 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500168 free(grab);
169}
170
171static const struct wl_grab_interface resize_grab_interface = {
172 resize_grab_motion,
173 resize_grab_button,
174 resize_grab_end
175};
176
Kristian Høgsberg07937562011-04-12 17:25:42 -0400177static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500178shell_resize(struct wl_client *client, struct wl_shell *shell,
179 struct wl_surface *surface,
180 struct wl_input_device *device, uint32_t time, uint32_t edges)
181{
182 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
183 struct wlsc_resize_grab *resize;
184 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
185 struct wlsc_surface *es = (struct wlsc_surface *) surface;
186
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500187 /* FIXME: Reject if fullscreen */
188
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500189 resize = malloc(sizeof *resize);
190 if (!resize) {
191 wl_client_post_no_memory(client);
192 return;
193 }
194
195 resize->grab.interface = &resize_grab_interface;
196 resize->edges = edges;
197 resize->dx = es->x - wd->input_device.grab_x;
198 resize->dy = es->y - wd->input_device.grab_y;
199 resize->width = es->width;
200 resize->height = es->height;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500201 resize->surface = es;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400202 resize->shell = shell;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500203
204 if (edges == 0 || edges > 15 ||
205 (edges & 3) == 3 || (edges & 12) == 12)
206 return;
207
208 switch (edges) {
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500209 case WL_SHELL_RESIZE_TOP:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500210 pointer = WLSC_POINTER_TOP;
211 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500212 case WL_SHELL_RESIZE_BOTTOM:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500213 pointer = WLSC_POINTER_BOTTOM;
214 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500215 case WL_SHELL_RESIZE_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500216 pointer = WLSC_POINTER_LEFT;
217 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500218 case WL_SHELL_RESIZE_TOP_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500219 pointer = WLSC_POINTER_TOP_LEFT;
220 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500221 case WL_SHELL_RESIZE_BOTTOM_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500222 pointer = WLSC_POINTER_BOTTOM_LEFT;
223 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500224 case WL_SHELL_RESIZE_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500225 pointer = WLSC_POINTER_RIGHT;
226 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500227 case WL_SHELL_RESIZE_TOP_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500228 pointer = WLSC_POINTER_TOP_RIGHT;
229 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500230 case WL_SHELL_RESIZE_BOTTOM_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500231 pointer = WLSC_POINTER_BOTTOM_RIGHT;
232 break;
233 }
234
235 if (wl_input_device_update_grab(&wd->input_device,
236 &resize->grab, surface, time) < 0)
237 return;
238
239 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500240 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500241}
242
243static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500244destroy_drag(struct wl_resource *resource, struct wl_client *client)
245{
246 struct wl_drag *drag =
247 container_of(resource, struct wl_drag, resource);
248
249 wl_list_remove(&drag->drag_focus_listener.link);
250 if (drag->grab.input_device)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400251 wl_input_device_end_grab(drag->grab.input_device,
252 wlsc_compositor_get_time());
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500253
254 free(drag);
255}
256
257
258static void
259wl_drag_set_pointer_focus(struct wl_drag *drag,
260 struct wl_surface *surface, uint32_t time,
261 int32_t x, int32_t y, int32_t sx, int32_t sy)
262{
263 char **p, **end;
264
265 if (drag->drag_focus == surface)
266 return;
267
268 if (drag->drag_focus &&
269 (!surface || drag->drag_focus->client != surface->client))
270 wl_client_post_event(drag->drag_focus->client,
271 &drag->drag_offer.object,
272 WL_DRAG_OFFER_POINTER_FOCUS,
273 time, NULL, 0, 0, 0, 0);
274
275 if (surface &&
276 (!drag->drag_focus ||
277 drag->drag_focus->client != surface->client)) {
278 wl_client_post_global(surface->client,
279 &drag->drag_offer.object);
280
281 end = drag->types.data + drag->types.size;
282 for (p = drag->types.data; p < end; p++)
283 wl_client_post_event(surface->client,
284 &drag->drag_offer.object,
285 WL_DRAG_OFFER_OFFER, *p);
286 }
287
288 if (surface) {
289 wl_client_post_event(surface->client,
290 &drag->drag_offer.object,
291 WL_DRAG_OFFER_POINTER_FOCUS,
292 time, surface,
293 x, y, sx, sy);
294
295 }
296
297 drag->drag_focus = surface;
298 drag->pointer_focus_time = time;
299 drag->target = NULL;
300
301 wl_list_remove(&drag->drag_focus_listener.link);
302 if (surface)
303 wl_list_insert(surface->destroy_listener_list.prev,
304 &drag->drag_focus_listener.link);
305}
306
307static void
308drag_offer_accept(struct wl_client *client,
309 struct wl_drag_offer *offer, uint32_t time, const char *type)
310{
311 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
312 char **p, **end;
313
314 /* If the client responds to drag pointer_focus or motion
315 * events after the pointer has left the surface, we just
316 * discard the accept requests. The drag source just won't
317 * get the corresponding 'target' events and eventually the
318 * next surface/root will start sending events. */
319 if (time < drag->pointer_focus_time)
320 return;
321
322 drag->target = client;
323 drag->type = NULL;
324 end = drag->types.data + drag->types.size;
325 for (p = drag->types.data; p < end; p++)
326 if (type && strcmp(*p, type) == 0)
327 drag->type = *p;
328
329 wl_client_post_event(drag->source->client, &drag->resource.object,
330 WL_DRAG_TARGET, drag->type);
331}
332
333static void
334drag_offer_receive(struct wl_client *client,
335 struct wl_drag_offer *offer, int fd)
336{
337 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
338
339 wl_client_post_event(drag->source->client, &drag->resource.object,
340 WL_DRAG_FINISH, fd);
341 close(fd);
342}
343
344static void
345drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
346{
347 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
348
349 wl_client_post_event(drag->source->client, &drag->resource.object,
350 WL_DRAG_REJECT);
351}
352
353static const struct wl_drag_offer_interface drag_offer_interface = {
354 drag_offer_accept,
355 drag_offer_receive,
356 drag_offer_reject
357};
358
359static void
360drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
361{
362 char **p;
363
364 p = wl_array_add(&drag->types, sizeof *p);
365 if (p)
366 *p = strdup(type);
367 if (!p || !*p)
368 wl_client_post_no_memory(client);
369}
370
371static void
372drag_grab_motion(struct wl_grab *grab,
373 uint32_t time, int32_t x, int32_t y)
374{
375 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
376 struct wlsc_surface *es;
377 int32_t sx, sy;
378
379 es = pick_surface(grab->input_device, &sx, &sy);
380 wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy);
381 if (es)
382 wl_client_post_event(es->surface.client,
383 &drag->drag_offer.object,
384 WL_DRAG_OFFER_MOTION,
385 time, x, y, sx, sy);
386}
387
388static void
389drag_grab_button(struct wl_grab *grab,
390 uint32_t time, int32_t button, int32_t state)
391{
392}
393
394static void
395drag_grab_end(struct wl_grab *grab, uint32_t time)
396{
397 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500398 struct wlsc_surface *es;
399 struct wl_input_device *device = grab->input_device;
400 int32_t sx, sy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500401
402 if (drag->target)
403 wl_client_post_event(drag->target,
404 &drag->drag_offer.object,
405 WL_DRAG_OFFER_DROP);
406
407 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500408
409 es = pick_surface(grab->input_device, &sx, &sy);
410 wl_input_device_set_pointer_focus(device,
411 &es->surface, time,
412 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500413}
414
415static const struct wl_grab_interface drag_grab_interface = {
416 drag_grab_motion,
417 drag_grab_button,
418 drag_grab_end
419};
420
421static void
422drag_activate(struct wl_client *client,
423 struct wl_drag *drag,
424 struct wl_surface *surface,
425 struct wl_input_device *device, uint32_t time)
426{
427 struct wl_display *display = wl_client_get_display (client);
428 struct wlsc_surface *target;
429 int32_t sx, sy;
430
431 if (wl_input_device_update_grab(device,
432 &drag->grab, surface, time) < 0)
433 return;
434
435 drag->grab.interface = &drag_grab_interface;
436
437 drag->source = surface;
438
439 drag->drag_offer.object.interface = &wl_drag_offer_interface;
440 drag->drag_offer.object.implementation =
441 (void (**)(void)) &drag_offer_interface;
442
443 wl_display_add_object(display, &drag->drag_offer.object);
444
445 target = pick_surface(device, &sx, &sy);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500446 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500447 wl_drag_set_pointer_focus(drag, &target->surface, time,
448 device->x, device->y, sx, sy);
449}
450
451static void
452drag_destroy(struct wl_client *client, struct wl_drag *drag)
453{
454 wl_resource_destroy(&drag->resource, client);
455}
456
457static const struct wl_drag_interface drag_interface = {
458 drag_offer,
459 drag_activate,
460 drag_destroy,
461};
462
463static void
464drag_handle_surface_destroy(struct wl_listener *listener,
465 struct wl_surface *surface, uint32_t time)
466{
467 struct wl_drag *drag =
468 container_of(listener, struct wl_drag, drag_focus_listener);
469
470 if (drag->drag_focus == surface)
471 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
472}
473
474static void
475shell_create_drag(struct wl_client *client,
476 struct wl_shell *shell, uint32_t id)
477{
478 struct wl_drag *drag;
479
480 drag = malloc(sizeof *drag);
481 if (drag == NULL) {
482 wl_client_post_no_memory(client);
483 return;
484 }
485
486 memset(drag, 0, sizeof *drag);
487 drag->resource.object.id = id;
488 drag->resource.object.interface = &wl_drag_interface;
489 drag->resource.object.implementation =
490 (void (**)(void)) &drag_interface;
491
492 drag->resource.destroy = destroy_drag;
493
494 drag->drag_focus_listener.func = drag_handle_surface_destroy;
495 wl_list_init(&drag->drag_focus_listener.link);
496
497 wl_client_add_resource(client, &drag->resource);
498}
499
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500500void
501wlsc_selection_set_focus(struct wl_selection *selection,
502 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);
581
582 selection->input_device = device;
583
584 selection->selection_offer.object.interface =
585 &wl_selection_offer_interface;
586 selection->selection_offer.object.implementation =
587 (void (**)(void)) &selection_offer_interface;
588
589 wl_display_add_object(display, &selection->selection_offer.object);
590
591 if (wd->selection) {
592 wl_client_post_event(wd->selection->client,
593 &wd->selection->resource.object,
594 WL_SELECTION_CANCELLED);
595 }
596 wd->selection = selection;
597
598 wlsc_selection_set_focus(selection, device->keyboard_focus, time);
599}
600
601static void
602selection_destroy(struct wl_client *client, struct wl_selection *selection)
603{
604 wl_resource_destroy(&selection->resource, client);
605}
606
607static const struct wl_selection_interface selection_interface = {
608 selection_offer,
609 selection_activate,
610 selection_destroy
611};
612
613static void
614destroy_selection(struct wl_resource *resource, struct wl_client *client)
615{
616 struct wl_selection *selection =
617 container_of(resource, struct wl_selection, resource);
618 struct wlsc_input_device *wd =
619 (struct wlsc_input_device *) selection->input_device;
620
621 if (wd && wd->selection == selection) {
622 wd->selection = NULL;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400623 wlsc_selection_set_focus(selection, NULL,
624 wlsc_compositor_get_time());
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500625 }
626
627 wl_list_remove(&selection->selection_focus_listener.link);
628 free(selection);
629}
630
631static void
632selection_handle_surface_destroy(struct wl_listener *listener,
633 struct wl_surface *surface, uint32_t time)
634{
635}
636
637static void
638shell_create_selection(struct wl_client *client,
639 struct wl_shell *shell, uint32_t id)
640{
641 struct wl_selection *selection;
642
643 selection = malloc(sizeof *selection);
644 if (selection == NULL) {
645 wl_client_post_no_memory(client);
646 return;
647 }
648
649 memset(selection, 0, sizeof *selection);
650 selection->resource.object.id = id;
651 selection->resource.object.interface = &wl_selection_interface;
652 selection->resource.object.implementation =
653 (void (**)(void)) &selection_interface;
654
655 selection->client = client;
656 selection->resource.destroy = destroy_selection;
657 selection->selection_focus = NULL;
658
659 selection->selection_focus_listener.func =
660 selection_handle_surface_destroy;
661 wl_list_init(&selection->selection_focus_listener.link);
662
663 wl_client_add_resource(client, &selection->resource);
664}
665
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500666const static struct wl_shell_interface shell_interface = {
667 shell_move,
668 shell_resize,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500669 shell_create_drag,
670 shell_create_selection
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500671};
672
Kristian Høgsberg07937562011-04-12 17:25:42 -0400673static void
674move_binding(struct wl_input_device *device, uint32_t time,
675 uint32_t key, uint32_t button, uint32_t state, void *data)
676{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400677 struct wl_shell *shell = data;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400678 struct wlsc_surface *surface =
679 (struct wlsc_surface *) device->pointer_focus;
680
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400681 if (surface == NULL)
682 return;
683
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400684 shell_move(NULL, shell, &surface->surface, device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400685}
686
687static void
688resize_binding(struct wl_input_device *device, uint32_t time,
689 uint32_t key, uint32_t button, uint32_t state, void *data)
690{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400691 struct wl_shell *shell = data;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400692 struct wlsc_surface *surface =
693 (struct wlsc_surface *) device->pointer_focus;
694 uint32_t edges = 0;
695 int32_t x, y;
696
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400697 if (surface == NULL)
698 return;
699
Kristian Høgsberg07937562011-04-12 17:25:42 -0400700 x = device->grab_x - surface->x;
701 y = device->grab_y - surface->y;
702
703 if (x < surface->width / 3)
704 edges |= WL_SHELL_RESIZE_LEFT;
705 else if (x < 2 * surface->width / 3)
706 edges |= 0;
707 else
708 edges |= WL_SHELL_RESIZE_RIGHT;
709
710 if (y < surface->height / 3)
711 edges |= WL_SHELL_RESIZE_TOP;
712 else if (y < 2 * surface->height / 3)
713 edges |= 0;
714 else
715 edges |= WL_SHELL_RESIZE_BOTTOM;
716
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400717 shell_resize(NULL, shell, &surface->surface, device, time, edges);
718}
719
720static void
721lock(struct wlsc_shell *shell)
722{
723}
724
725static void
726attach(struct wlsc_shell *shell, struct wlsc_surface *surface)
727{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400728}
729
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500730int
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400731desktop_shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500732{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400733 struct wl_shell *shell;
734
735 shell = malloc(sizeof *shell);
736 if (shell == NULL)
737 return -1;
738
739 shell->shell.lock = lock;
740 shell->shell.attach = attach;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500741
742 shell->object.interface = &wl_shell_interface;
743 shell->object.implementation = (void (**)(void)) &shell_interface;
744 wl_display_add_object(ec->wl_display, &shell->object);
745 if (wl_display_add_global(ec->wl_display, &shell->object, NULL))
746 return -1;
747
Kristian Høgsberg07937562011-04-12 17:25:42 -0400748 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400749 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400750 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400751 resize_binding, shell);
752
753 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400754
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500755 return 0;
756}