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.
26 #include <linux/input.h>
28 #include "wayland-server.h"
29 #include "compositor.h"
32 struct wl_object object;
33 struct wlsc_shell shell;
36 struct wlsc_move_grab {
38 struct wlsc_surface *surface;
43 move_grab_motion(struct wl_grab *grab,
44 uint32_t time, int32_t x, int32_t y)
46 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
47 struct wlsc_surface *es = move->surface;
49 wlsc_surface_configure(es, x + move->dx, y + move->dy,
50 es->width, es->height);
54 move_grab_button(struct wl_grab *grab,
55 uint32_t time, int32_t button, int32_t state)
60 move_grab_end(struct wl_grab *grab, uint32_t time)
62 struct wlsc_surface *es;
63 struct wl_input_device *device = grab->input_device;
66 es = pick_surface(grab->input_device, &sx, &sy);
67 wl_input_device_set_pointer_focus(device,
69 device->x, device->y, sx, sy);
73 static const struct wl_grab_interface move_grab_interface = {
80 shell_move(struct wl_client *client, struct wl_resource *resource,
81 struct wl_surface *surface,
82 struct wl_input_device *device, uint32_t time)
84 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
85 struct wlsc_surface *es = (struct wlsc_surface *) surface;
86 struct wlsc_move_grab *move;
88 /* FIXME: Reject if fullscreen */
90 move = malloc(sizeof *move);
92 wl_client_post_no_memory(client);
96 move->grab.interface = &move_grab_interface;
97 move->dx = es->x - wd->input_device.grab_x;
98 move->dy = es->y - wd->input_device.grab_y;
101 if (wl_input_device_update_grab(&wd->input_device,
102 &move->grab, surface, time) < 0)
105 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
106 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
109 struct wlsc_resize_grab {
112 int32_t dx, dy, width, height;
113 struct wlsc_surface *surface;
114 struct wl_shell *shell;
115 struct wl_resource resource;
119 resize_grab_motion(struct wl_grab *grab,
120 uint32_t time, int32_t x, int32_t y)
122 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
123 struct wl_input_device *device = grab->input_device;
124 struct wl_surface *surface = &resize->surface->surface;
125 int32_t width, height;
127 if (resize->edges & WL_SHELL_RESIZE_LEFT) {
128 width = device->grab_x - x + resize->width;
129 } else if (resize->edges & WL_SHELL_RESIZE_RIGHT) {
130 width = x - device->grab_x + resize->width;
132 width = resize->width;
135 if (resize->edges & WL_SHELL_RESIZE_TOP) {
136 height = device->grab_y - y + resize->height;
137 } else if (resize->edges & WL_SHELL_RESIZE_BOTTOM) {
138 height = y - device->grab_y + resize->height;
140 height = resize->height;
143 wl_resource_post_event(&resize->resource,
144 WL_SHELL_CONFIGURE, time, resize->edges,
145 surface, width, height);
149 resize_grab_button(struct wl_grab *grab,
150 uint32_t time, int32_t button, int32_t state)
155 resize_grab_end(struct wl_grab *grab, uint32_t time)
157 struct wlsc_surface *es;
158 struct wl_input_device *device = grab->input_device;
161 es = pick_surface(grab->input_device, &sx, &sy);
162 wl_input_device_set_pointer_focus(device,
164 device->x, device->y, sx, sy);
168 static const struct wl_grab_interface resize_grab_interface = {
175 shell_resize(struct wl_client *client, struct wl_resource *resource,
176 struct wl_surface *surface,
177 struct wl_input_device *device, uint32_t time, uint32_t edges)
179 struct wl_shell *shell = resource->data;
180 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
181 struct wlsc_resize_grab *resize;
182 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
183 struct wlsc_surface *es = (struct wlsc_surface *) surface;
185 /* FIXME: Reject if fullscreen */
187 resize = malloc(sizeof *resize);
189 wl_client_post_no_memory(client);
193 resize->grab.interface = &resize_grab_interface;
194 resize->edges = edges;
195 resize->dx = es->x - wd->input_device.grab_x;
196 resize->dy = es->y - wd->input_device.grab_y;
197 resize->width = es->width;
198 resize->height = es->height;
199 resize->surface = es;
200 resize->shell = shell;
202 resize->resource.object = resource->object;
203 resize->resource.client = client;
205 if (edges == 0 || edges > 15 ||
206 (edges & 3) == 3 || (edges & 12) == 12)
210 case WL_SHELL_RESIZE_TOP:
211 pointer = WLSC_POINTER_TOP;
213 case WL_SHELL_RESIZE_BOTTOM:
214 pointer = WLSC_POINTER_BOTTOM;
216 case WL_SHELL_RESIZE_LEFT:
217 pointer = WLSC_POINTER_LEFT;
219 case WL_SHELL_RESIZE_TOP_LEFT:
220 pointer = WLSC_POINTER_TOP_LEFT;
222 case WL_SHELL_RESIZE_BOTTOM_LEFT:
223 pointer = WLSC_POINTER_BOTTOM_LEFT;
225 case WL_SHELL_RESIZE_RIGHT:
226 pointer = WLSC_POINTER_RIGHT;
228 case WL_SHELL_RESIZE_TOP_RIGHT:
229 pointer = WLSC_POINTER_TOP_RIGHT;
231 case WL_SHELL_RESIZE_BOTTOM_RIGHT:
232 pointer = WLSC_POINTER_BOTTOM_RIGHT;
236 if (wl_input_device_update_grab(&wd->input_device,
237 &resize->grab, surface, time) < 0)
240 wlsc_input_device_set_pointer_image(wd, pointer);
241 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
245 shell_set_toplevel(struct wl_client *client,
246 struct wl_resource *resource,
247 struct wl_surface *surface)
250 struct wlsc_surface *es = (struct wlsc_surface *) surface;
251 struct wlsc_compositor *ec = es->compositor;
253 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN) {
256 } else if (es->map_type == WLSC_SURFACE_MAP_UNMAPPED) {
257 es->x = 10 + random() % 400;
258 es->y = 10 + random() % 400;
259 /* assign to first output */
260 es->output = container_of(ec->output_list.next,
261 struct wlsc_output, link);
264 wlsc_surface_damage(es);
265 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
266 es->fullscreen_output = NULL;
270 shell_set_transient(struct wl_client *client,
271 struct wl_resource *resource,
272 struct wl_surface *surface,
273 struct wl_surface *parent,
274 int x, int y, uint32_t flags)
276 struct wlsc_surface *es = (struct wlsc_surface *) surface;
277 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
279 /* assign to parents output */
280 es->output = pes->output;
285 wlsc_surface_damage(es);
286 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
290 shell_set_fullscreen(struct wl_client *client,
291 struct wl_resource *resource,
292 struct wl_surface *surface)
295 struct wlsc_surface *es = (struct wlsc_surface *) surface;
296 struct wlsc_output *output;
298 /* FIXME: Fullscreen on first output */
299 /* FIXME: Handle output going away */
300 output = container_of(es->compositor->output_list.next,
301 struct wlsc_output, link);
306 es->x = (output->current->width - es->width) / 2;
307 es->y = (output->current->height - es->height) / 2;
308 es->fullscreen_output = output;
309 wlsc_surface_damage(es);
310 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
314 destroy_drag(struct wl_resource *resource)
316 struct wl_drag *drag =
317 container_of(resource, struct wl_drag, resource);
319 wl_list_remove(&drag->drag_focus_listener.link);
320 if (drag->grab.input_device)
321 wl_input_device_end_grab(drag->grab.input_device,
322 wlsc_compositor_get_time());
329 wl_drag_set_pointer_focus(struct wl_drag *drag,
330 struct wl_surface *surface, uint32_t time,
331 int32_t x, int32_t y, int32_t sx, int32_t sy)
335 if (drag->drag_focus == surface)
338 if (drag->drag_focus &&
340 drag->drag_focus->resource.client != surface->resource.client))
341 wl_resource_post_event(&drag->drag_offer.resource,
342 WL_DRAG_OFFER_POINTER_FOCUS,
343 time, NULL, 0, 0, 0, 0);
346 (!drag->drag_focus ||
347 drag->drag_focus->resource.client != surface->resource.client)) {
348 wl_client_post_global(surface->resource.client,
349 &drag->drag_offer.resource.object);
351 drag->drag_offer.resource.client = surface->resource.client;
352 end = drag->types.data + drag->types.size;
353 for (p = drag->types.data; p < end; p++)
354 wl_resource_post_event(&drag->drag_offer.resource,
355 WL_DRAG_OFFER_OFFER, *p);
359 wl_resource_post_event(&drag->drag_offer.resource,
360 WL_DRAG_OFFER_POINTER_FOCUS,
366 drag->drag_focus = surface;
367 drag->pointer_focus_time = time;
370 wl_list_remove(&drag->drag_focus_listener.link);
372 wl_list_insert(surface->resource.destroy_listener_list.prev,
373 &drag->drag_focus_listener.link);
377 drag_offer_accept(struct wl_client *client, struct wl_resource *resource,
378 uint32_t time, const char *type)
380 struct wl_drag_offer *offer = resource->data;
381 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
384 /* If the client responds to drag pointer_focus or motion
385 * events after the pointer has left the surface, we just
386 * discard the accept requests. The drag source just won't
387 * get the corresponding 'target' events and eventually the
388 * next surface/root will start sending events. */
389 if (time < drag->pointer_focus_time)
392 drag->target = client;
394 end = drag->types.data + drag->types.size;
395 for (p = drag->types.data; p < end; p++)
396 if (type && strcmp(*p, type) == 0)
399 wl_resource_post_event(&drag->resource, WL_DRAG_TARGET, drag->type);
403 drag_offer_receive(struct wl_client *client,
404 struct wl_resource *resource, int fd)
406 struct wl_drag_offer *offer = resource->data;
407 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
409 wl_resource_post_event(&drag->resource, WL_DRAG_FINISH, fd);
414 drag_offer_reject(struct wl_client *client, struct wl_resource *resource)
416 struct wl_drag_offer *offer = resource->data;
417 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
419 wl_resource_post_event(&drag->resource, WL_DRAG_REJECT);
422 static const struct wl_drag_offer_interface drag_offer_interface = {
429 drag_offer(struct wl_client *client,
430 struct wl_resource *resource, const char *type)
432 struct wl_drag *drag = resource->data;
435 p = wl_array_add(&drag->types, sizeof *p);
439 wl_client_post_no_memory(client);
443 drag_grab_motion(struct wl_grab *grab,
444 uint32_t time, int32_t x, int32_t y)
446 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
447 struct wlsc_surface *es;
450 es = pick_surface(grab->input_device, &sx, &sy);
451 wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy);
453 wl_resource_post_event(&drag->drag_offer.resource,
454 WL_DRAG_OFFER_MOTION,
459 drag_grab_button(struct wl_grab *grab,
460 uint32_t time, int32_t button, int32_t state)
465 drag_grab_end(struct wl_grab *grab, uint32_t time)
467 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
468 struct wlsc_surface *es;
469 struct wl_input_device *device = grab->input_device;
473 wl_resource_post_event(&drag->drag_offer.resource,
476 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
478 es = pick_surface(grab->input_device, &sx, &sy);
479 wl_input_device_set_pointer_focus(device,
481 device->x, device->y, sx, sy);
484 static const struct wl_grab_interface drag_grab_interface = {
491 drag_activate(struct wl_client *client,
492 struct wl_resource *resource,
493 struct wl_surface *surface,
494 struct wl_input_device *device, uint32_t time)
496 struct wl_drag *drag = resource->data;
497 struct wl_display *display = wl_client_get_display (client);
498 struct wlsc_surface *target;
501 if (wl_input_device_update_grab(device,
502 &drag->grab, surface, time) < 0)
505 drag->grab.interface = &drag_grab_interface;
507 drag->source = surface;
509 drag->drag_offer.resource.object.interface = &wl_drag_offer_interface;
510 drag->drag_offer.resource.object.implementation =
511 (void (**)(void)) &drag_offer_interface;
513 wl_display_add_global(display,
514 &drag->drag_offer.resource.object, NULL);
516 target = pick_surface(device, &sx, &sy);
517 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
518 wl_drag_set_pointer_focus(drag, &target->surface, time,
519 device->x, device->y, sx, sy);
523 drag_destroy(struct wl_client *client, struct wl_resource *resource)
525 wl_resource_destroy(resource, wlsc_compositor_get_time());
528 static const struct wl_drag_interface drag_interface = {
535 drag_handle_surface_destroy(struct wl_listener *listener,
536 struct wl_resource *resource, uint32_t time)
538 struct wl_drag *drag =
539 container_of(listener, struct wl_drag, drag_focus_listener);
540 struct wl_surface *surface = (struct wl_surface *) resource;
542 if (drag->drag_focus == surface)
543 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
547 shell_create_drag(struct wl_client *client,
548 struct wl_resource *resource, uint32_t id)
550 struct wl_drag *drag;
552 drag = malloc(sizeof *drag);
554 wl_client_post_no_memory(client);
558 memset(drag, 0, sizeof *drag);
559 drag->resource.object.id = id;
560 drag->resource.object.interface = &wl_drag_interface;
561 drag->resource.object.implementation =
562 (void (**)(void)) &drag_interface;
564 drag->resource.destroy = destroy_drag;
566 drag->drag_focus_listener.func = drag_handle_surface_destroy;
567 wl_list_init(&drag->drag_focus_listener.link);
569 wl_client_add_resource(client, &drag->resource);
573 wlsc_selection_set_focus(struct wlsc_shell *shell,
574 struct wl_selection *selection,
575 struct wl_surface *surface, uint32_t time)
579 if (selection->selection_focus == surface)
582 if (selection->selection_focus != NULL)
583 wl_resource_post_event(&selection->selection_offer.resource,
584 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
588 wl_client_post_global(surface->resource.client,
589 &selection->selection_offer.resource.object);
591 selection->selection_offer.resource.client = surface->resource.client;
592 end = selection->types.data + selection->types.size;
593 for (p = selection->types.data; p < end; p++)
594 wl_resource_post_event(&selection->selection_offer.resource,
595 WL_SELECTION_OFFER_OFFER, *p);
597 wl_list_remove(&selection->selection_focus_listener.link);
598 wl_list_insert(surface->resource.destroy_listener_list.prev,
599 &selection->selection_focus_listener.link);
601 wl_resource_post_event(&selection->selection_offer.resource,
602 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
603 selection->input_device);
606 selection->selection_focus = surface;
608 wl_list_remove(&selection->selection_focus_listener.link);
610 wl_list_insert(surface->resource.destroy_listener_list.prev,
611 &selection->selection_focus_listener.link);
615 selection_offer_receive(struct wl_client *client,
616 struct wl_resource *resource,
617 const char *mime_type, int fd)
619 struct wl_selection_offer *offer = resource->data;
620 struct wl_selection *selection =
621 container_of(offer, struct wl_selection, selection_offer);
623 wl_resource_post_event(&selection->resource,
624 WL_SELECTION_SEND, mime_type, fd);
628 static const struct wl_selection_offer_interface selection_offer_interface = {
629 selection_offer_receive
633 selection_offer(struct wl_client *client,
634 struct wl_resource *resource, const char *type)
636 struct wl_selection *selection = resource->data;
639 p = wl_array_add(&selection->types, sizeof *p);
643 wl_client_post_no_memory(client);
647 selection_activate(struct wl_client *client,
648 struct wl_resource *resource,
649 struct wl_input_device *device, uint32_t time)
651 struct wl_selection *selection = resource->data;
652 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
653 struct wl_display *display = wl_client_get_display (client);
654 struct wlsc_compositor *compositor =
655 (struct wlsc_compositor *) device->compositor;
657 selection->input_device = device;
659 selection->selection_offer.resource.object.interface =
660 &wl_selection_offer_interface;
661 selection->selection_offer.resource.object.implementation =
662 (void (**)(void)) &selection_offer_interface;
664 wl_display_add_global(display,
665 &selection->selection_offer.resource.object,
669 wl_resource_post_event(&wd->selection->resource,
670 WL_SELECTION_CANCELLED);
672 wd->selection = selection;
674 wlsc_selection_set_focus(compositor->shell,
675 selection, device->keyboard_focus, time);
679 selection_destroy(struct wl_client *client, struct wl_resource *resource)
681 wl_resource_destroy(resource, wlsc_compositor_get_time());
684 static const struct wl_selection_interface selection_interface = {
691 destroy_selection(struct wl_resource *resource)
693 struct wl_selection *selection =
694 container_of(resource, struct wl_selection, resource);
695 struct wlsc_input_device *wd =
696 (struct wlsc_input_device *) selection->input_device;
697 struct wlsc_compositor *compositor =
698 (struct wlsc_compositor *) wd->input_device.compositor;
700 if (wd && wd->selection == selection) {
701 wd->selection = NULL;
702 wlsc_selection_set_focus(compositor->shell,
704 wlsc_compositor_get_time());
707 wl_list_remove(&selection->selection_focus_listener.link);
712 selection_handle_surface_destroy(struct wl_listener *listener,
713 struct wl_resource *resource, uint32_t time)
718 shell_create_selection(struct wl_client *client,
719 struct wl_resource *resource, uint32_t id)
721 struct wl_selection *selection;
723 selection = malloc(sizeof *selection);
724 if (selection == NULL) {
725 wl_client_post_no_memory(client);
729 memset(selection, 0, sizeof *selection);
730 selection->resource.object.id = id;
731 selection->resource.object.interface = &wl_selection_interface;
732 selection->resource.object.implementation =
733 (void (**)(void)) &selection_interface;
735 selection->client = client;
736 selection->resource.destroy = destroy_selection;
737 selection->selection_focus = NULL;
739 selection->selection_focus_listener.func =
740 selection_handle_surface_destroy;
741 wl_list_init(&selection->selection_focus_listener.link);
743 wl_client_add_resource(client, &selection->resource);
746 const static struct wl_shell_interface shell_interface = {
750 shell_create_selection,
757 move_binding(struct wl_input_device *device, uint32_t time,
758 uint32_t key, uint32_t button, uint32_t state, void *data)
760 struct wlsc_surface *surface =
761 (struct wlsc_surface *) device->pointer_focus;
766 shell_move(NULL, NULL, &surface->surface, device, time);
770 resize_binding(struct wl_input_device *device, uint32_t time,
771 uint32_t key, uint32_t button, uint32_t state, void *data)
773 struct wlsc_surface *surface =
774 (struct wlsc_surface *) device->pointer_focus;
781 x = device->grab_x - surface->x;
782 y = device->grab_y - surface->y;
784 if (x < surface->width / 3)
785 edges |= WL_SHELL_RESIZE_LEFT;
786 else if (x < 2 * surface->width / 3)
789 edges |= WL_SHELL_RESIZE_RIGHT;
791 if (y < surface->height / 3)
792 edges |= WL_SHELL_RESIZE_TOP;
793 else if (y < 2 * surface->height / 3)
796 edges |= WL_SHELL_RESIZE_BOTTOM;
798 shell_resize(NULL, NULL, &surface->surface, device, time, edges);
802 lock(struct wlsc_shell *shell)
807 attach(struct wlsc_shell *shell, struct wlsc_surface *es)
809 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN) {
810 es->x = (es->fullscreen_output->current->width - es->width) / 2;
811 es->y = (es->fullscreen_output->current->height - es->height) / 2;
816 shell_init(struct wlsc_compositor *ec);
819 shell_init(struct wlsc_compositor *ec)
821 struct wl_shell *shell;
823 shell = malloc(sizeof *shell);
827 shell->shell.lock = lock;
828 shell->shell.attach = attach;
829 shell->shell.set_selection_focus = wlsc_selection_set_focus;
831 shell->object.interface = &wl_shell_interface;
832 shell->object.implementation = (void (**)(void)) &shell_interface;
833 if (wl_display_add_global(ec->wl_display, &shell->object, NULL))
836 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
837 move_binding, shell);
838 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
839 resize_binding, shell);
841 ec->shell = &shell->shell;