2 * Copyright © 2010 Intel Corporation
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.
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.
27 #include "wayland-server.h"
28 #include "compositor.h"
30 struct wlsc_move_grab {
32 struct wlsc_surface *surface;
37 move_grab_motion(struct wl_grab *grab,
38 uint32_t time, int32_t x, int32_t y)
40 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
41 struct wlsc_surface *es = move->surface;
43 wlsc_surface_damage(es);
46 wlsc_surface_update_matrix(es);
47 wlsc_surface_damage(es);
51 move_grab_button(struct wl_grab *grab,
52 uint32_t time, int32_t button, int32_t state)
57 move_grab_end(struct wl_grab *grab, uint32_t time)
59 struct wlsc_surface *es;
60 struct wl_input_device *device = grab->input_device;
63 es = pick_surface(grab->input_device, &sx, &sy);
64 wl_input_device_set_pointer_focus(device,
66 device->x, device->y, sx, sy);
70 static const struct wl_grab_interface move_grab_interface = {
77 shell_move(struct wl_client *client, struct wl_shell *shell,
78 struct wl_surface *surface,
79 struct wl_input_device *device, uint32_t time)
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;
85 /* FIXME: Reject if fullscreen */
87 move = malloc(sizeof *move);
89 wl_client_post_no_memory(client);
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;
98 if (wl_input_device_update_grab(&wd->input_device,
99 &move->grab, surface, time) < 0)
102 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
103 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
106 struct wlsc_resize_grab {
109 int32_t dx, dy, width, height;
110 struct wlsc_surface *surface;
114 resize_grab_motion(struct wl_grab *grab,
115 uint32_t time, int32_t x, int32_t y)
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;
121 struct wl_surface *surface = &resize->surface->surface;
122 int32_t width, height;
124 if (resize->edges & WL_SHELL_RESIZE_LEFT) {
125 width = device->grab_x - x + resize->width;
126 } else if (resize->edges & WL_SHELL_RESIZE_RIGHT) {
127 width = x - device->grab_x + resize->width;
129 width = resize->width;
132 if (resize->edges & WL_SHELL_RESIZE_TOP) {
133 height = device->grab_y - y + resize->height;
134 } else if (resize->edges & WL_SHELL_RESIZE_BOTTOM) {
135 height = y - device->grab_y + resize->height;
137 height = resize->height;
140 wl_client_post_event(surface->client, &ec->shell.object,
141 WL_SHELL_CONFIGURE, time, resize->edges,
142 surface, width, height);
146 resize_grab_button(struct wl_grab *grab,
147 uint32_t time, int32_t button, int32_t state)
152 resize_grab_end(struct wl_grab *grab, uint32_t time)
154 struct wlsc_surface *es;
155 struct wl_input_device *device = grab->input_device;
158 es = pick_surface(grab->input_device, &sx, &sy);
159 wl_input_device_set_pointer_focus(device,
161 device->x, device->y, sx, sy);
165 static const struct wl_grab_interface resize_grab_interface = {
172 shell_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)
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;
181 /* FIXME: Reject if fullscreen */
183 resize = malloc(sizeof *resize);
185 wl_client_post_no_memory(client);
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;
195 resize->surface = es;
197 if (edges == 0 || edges > 15 ||
198 (edges & 3) == 3 || (edges & 12) == 12)
202 case WL_SHELL_RESIZE_TOP:
203 pointer = WLSC_POINTER_TOP;
205 case WL_SHELL_RESIZE_BOTTOM:
206 pointer = WLSC_POINTER_BOTTOM;
208 case WL_SHELL_RESIZE_LEFT:
209 pointer = WLSC_POINTER_LEFT;
211 case WL_SHELL_RESIZE_TOP_LEFT:
212 pointer = WLSC_POINTER_TOP_LEFT;
214 case WL_SHELL_RESIZE_BOTTOM_LEFT:
215 pointer = WLSC_POINTER_BOTTOM_LEFT;
217 case WL_SHELL_RESIZE_RIGHT:
218 pointer = WLSC_POINTER_RIGHT;
220 case WL_SHELL_RESIZE_TOP_RIGHT:
221 pointer = WLSC_POINTER_TOP_RIGHT;
223 case WL_SHELL_RESIZE_BOTTOM_RIGHT:
224 pointer = WLSC_POINTER_BOTTOM_RIGHT;
228 if (wl_input_device_update_grab(&wd->input_device,
229 &resize->grab, surface, time) < 0)
232 wlsc_input_device_set_pointer_image(wd, pointer);
233 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
237 destroy_drag(struct wl_resource *resource, struct wl_client *client)
239 struct wl_drag *drag =
240 container_of(resource, struct wl_drag, resource);
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());
251 wl_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)
257 if (drag->drag_focus == surface)
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);
268 (!drag->drag_focus ||
269 drag->drag_focus->client != surface->client)) {
270 wl_client_post_global(surface->client,
271 &drag->drag_offer.object);
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);
281 wl_client_post_event(surface->client,
282 &drag->drag_offer.object,
283 WL_DRAG_OFFER_POINTER_FOCUS,
289 drag->drag_focus = surface;
290 drag->pointer_focus_time = time;
293 wl_list_remove(&drag->drag_focus_listener.link);
295 wl_list_insert(surface->destroy_listener_list.prev,
296 &drag->drag_focus_listener.link);
300 drag_offer_accept(struct wl_client *client,
301 struct wl_drag_offer *offer, uint32_t time, const char *type)
303 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
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)
314 drag->target = client;
316 end = drag->types.data + drag->types.size;
317 for (p = drag->types.data; p < end; p++)
318 if (type && strcmp(*p, type) == 0)
321 wl_client_post_event(drag->source->client, &drag->resource.object,
322 WL_DRAG_TARGET, drag->type);
326 drag_offer_receive(struct wl_client *client,
327 struct wl_drag_offer *offer, int fd)
329 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
331 wl_client_post_event(drag->source->client, &drag->resource.object,
337 drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
339 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
341 wl_client_post_event(drag->source->client, &drag->resource.object,
345 static const struct wl_drag_offer_interface drag_offer_interface = {
352 drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
356 p = wl_array_add(&drag->types, sizeof *p);
360 wl_client_post_no_memory(client);
364 drag_grab_motion(struct wl_grab *grab,
365 uint32_t time, int32_t x, int32_t y)
367 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
368 struct wlsc_surface *es;
371 es = pick_surface(grab->input_device, &sx, &sy);
372 wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy);
374 wl_client_post_event(es->surface.client,
375 &drag->drag_offer.object,
376 WL_DRAG_OFFER_MOTION,
381 drag_grab_button(struct wl_grab *grab,
382 uint32_t time, int32_t button, int32_t state)
387 drag_grab_end(struct wl_grab *grab, uint32_t time)
389 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
390 struct wlsc_surface *es;
391 struct wl_input_device *device = grab->input_device;
395 wl_client_post_event(drag->target,
396 &drag->drag_offer.object,
399 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
401 es = pick_surface(grab->input_device, &sx, &sy);
402 wl_input_device_set_pointer_focus(device,
404 device->x, device->y, sx, sy);
407 static const struct wl_grab_interface drag_grab_interface = {
414 drag_activate(struct wl_client *client,
415 struct wl_drag *drag,
416 struct wl_surface *surface,
417 struct wl_input_device *device, uint32_t time)
419 struct wl_display *display = wl_client_get_display (client);
420 struct wlsc_surface *target;
423 if (wl_input_device_update_grab(device,
424 &drag->grab, surface, time) < 0)
427 drag->grab.interface = &drag_grab_interface;
429 drag->source = surface;
431 drag->drag_offer.object.interface = &wl_drag_offer_interface;
432 drag->drag_offer.object.implementation =
433 (void (**)(void)) &drag_offer_interface;
435 wl_display_add_object(display, &drag->drag_offer.object);
437 target = pick_surface(device, &sx, &sy);
438 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
439 wl_drag_set_pointer_focus(drag, &target->surface, time,
440 device->x, device->y, sx, sy);
444 drag_destroy(struct wl_client *client, struct wl_drag *drag)
446 wl_resource_destroy(&drag->resource, client);
449 static const struct wl_drag_interface drag_interface = {
456 drag_handle_surface_destroy(struct wl_listener *listener,
457 struct wl_surface *surface, uint32_t time)
459 struct wl_drag *drag =
460 container_of(listener, struct wl_drag, drag_focus_listener);
462 if (drag->drag_focus == surface)
463 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
467 shell_create_drag(struct wl_client *client,
468 struct wl_shell *shell, uint32_t id)
470 struct wl_drag *drag;
472 drag = malloc(sizeof *drag);
474 wl_client_post_no_memory(client);
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;
484 drag->resource.destroy = destroy_drag;
486 drag->drag_focus_listener.func = drag_handle_surface_destroy;
487 wl_list_init(&drag->drag_focus_listener.link);
489 wl_client_add_resource(client, &drag->resource);
493 wlsc_selection_set_focus(struct wl_selection *selection,
494 struct wl_surface *surface, uint32_t time)
498 if (selection->selection_focus == surface)
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,
508 wl_client_post_global(surface->client,
509 &selection->selection_offer.object);
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);
517 wl_list_remove(&selection->selection_focus_listener.link);
518 wl_list_insert(surface->destroy_listener_list.prev,
519 &selection->selection_focus_listener.link);
521 wl_client_post_event(surface->client,
522 &selection->selection_offer.object,
523 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
524 selection->input_device);
527 selection->selection_focus = surface;
529 wl_list_remove(&selection->selection_focus_listener.link);
531 wl_list_insert(surface->destroy_listener_list.prev,
532 &selection->selection_focus_listener.link);
536 selection_offer_receive(struct wl_client *client,
537 struct wl_selection_offer *offer,
538 const char *mime_type, int fd)
540 struct wl_selection *selection =
541 container_of(offer, struct wl_selection, selection_offer);
543 wl_client_post_event(selection->client,
544 &selection->resource.object,
545 WL_SELECTION_SEND, mime_type, fd);
549 static const struct wl_selection_offer_interface selection_offer_interface = {
550 selection_offer_receive
554 selection_offer(struct wl_client *client,
555 struct wl_selection *selection, const char *type)
559 p = wl_array_add(&selection->types, sizeof *p);
563 wl_client_post_no_memory(client);
567 selection_activate(struct wl_client *client,
568 struct wl_selection *selection,
569 struct wl_input_device *device, uint32_t time)
571 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
572 struct wl_display *display = wl_client_get_display (client);
574 selection->input_device = device;
576 selection->selection_offer.object.interface =
577 &wl_selection_offer_interface;
578 selection->selection_offer.object.implementation =
579 (void (**)(void)) &selection_offer_interface;
581 wl_display_add_object(display, &selection->selection_offer.object);
584 wl_client_post_event(wd->selection->client,
585 &wd->selection->resource.object,
586 WL_SELECTION_CANCELLED);
588 wd->selection = selection;
590 wlsc_selection_set_focus(selection, device->keyboard_focus, time);
594 selection_destroy(struct wl_client *client, struct wl_selection *selection)
596 wl_resource_destroy(&selection->resource, client);
599 static const struct wl_selection_interface selection_interface = {
606 destroy_selection(struct wl_resource *resource, struct wl_client *client)
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;
613 if (wd && wd->selection == selection) {
614 wd->selection = NULL;
615 wlsc_selection_set_focus(selection, NULL, get_time());
618 wl_list_remove(&selection->selection_focus_listener.link);
623 selection_handle_surface_destroy(struct wl_listener *listener,
624 struct wl_surface *surface, uint32_t time)
629 shell_create_selection(struct wl_client *client,
630 struct wl_shell *shell, uint32_t id)
632 struct wl_selection *selection;
634 selection = malloc(sizeof *selection);
635 if (selection == NULL) {
636 wl_client_post_no_memory(client);
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;
646 selection->client = client;
647 selection->resource.destroy = destroy_selection;
648 selection->selection_focus = NULL;
650 selection->selection_focus_listener.func =
651 selection_handle_surface_destroy;
652 wl_list_init(&selection->selection_focus_listener.link);
654 wl_client_add_resource(client, &selection->resource);
657 const static struct wl_shell_interface shell_interface = {
661 shell_create_selection
665 wlsc_shell_init(struct wlsc_compositor *ec)
667 struct wl_shell *shell = &ec->shell;
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))