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_damage_below(es);
52 wlsc_surface_assign_output(es);
53 wlsc_surface_damage(es);
57 move_grab_button(struct wl_grab *grab,
58 uint32_t time, int32_t button, int32_t state)
63 move_grab_end(struct wl_grab *grab, uint32_t time)
65 struct wlsc_surface *es;
66 struct wl_input_device *device = grab->input_device;
69 es = pick_surface(grab->input_device, &sx, &sy);
70 wl_input_device_set_pointer_focus(device,
72 device->x, device->y, sx, sy);
76 static const struct wl_grab_interface move_grab_interface = {
83 shell_move(struct wl_client *client, struct wl_shell *shell,
84 struct wl_surface *surface,
85 struct wl_input_device *device, uint32_t time)
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;
91 /* FIXME: Reject if fullscreen */
93 move = malloc(sizeof *move);
95 wl_client_post_no_memory(client);
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;
104 if (wl_input_device_update_grab(&wd->input_device,
105 &move->grab, surface, time) < 0)
108 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
109 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
112 struct wlsc_resize_grab {
115 int32_t dx, dy, width, height;
116 struct wlsc_surface *surface;
117 struct wl_shell *shell;
121 resize_grab_motion(struct wl_grab *grab,
122 uint32_t time, int32_t x, int32_t y)
124 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
125 struct wl_input_device *device = grab->input_device;
126 struct wl_surface *surface = &resize->surface->surface;
127 int32_t width, height;
129 if (resize->edges & WL_SHELL_RESIZE_LEFT) {
130 width = device->grab_x - x + resize->width;
131 } else if (resize->edges & WL_SHELL_RESIZE_RIGHT) {
132 width = x - device->grab_x + resize->width;
134 width = resize->width;
137 if (resize->edges & WL_SHELL_RESIZE_TOP) {
138 height = device->grab_y - y + resize->height;
139 } else if (resize->edges & WL_SHELL_RESIZE_BOTTOM) {
140 height = y - device->grab_y + resize->height;
142 height = resize->height;
145 wl_client_post_event(surface->client, &resize->shell->object,
146 WL_SHELL_CONFIGURE, time, resize->edges,
147 surface, width, height);
151 resize_grab_button(struct wl_grab *grab,
152 uint32_t time, int32_t button, int32_t state)
157 resize_grab_end(struct wl_grab *grab, uint32_t time)
159 struct wlsc_surface *es;
160 struct wl_input_device *device = grab->input_device;
163 es = pick_surface(grab->input_device, &sx, &sy);
164 wl_input_device_set_pointer_focus(device,
166 device->x, device->y, sx, sy);
170 static const struct wl_grab_interface resize_grab_interface = {
177 shell_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)
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;
186 /* FIXME: Reject if fullscreen */
188 resize = malloc(sizeof *resize);
190 wl_client_post_no_memory(client);
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;
200 resize->surface = es;
201 resize->shell = shell;
203 if (edges == 0 || edges > 15 ||
204 (edges & 3) == 3 || (edges & 12) == 12)
208 case WL_SHELL_RESIZE_TOP:
209 pointer = WLSC_POINTER_TOP;
211 case WL_SHELL_RESIZE_BOTTOM:
212 pointer = WLSC_POINTER_BOTTOM;
214 case WL_SHELL_RESIZE_LEFT:
215 pointer = WLSC_POINTER_LEFT;
217 case WL_SHELL_RESIZE_TOP_LEFT:
218 pointer = WLSC_POINTER_TOP_LEFT;
220 case WL_SHELL_RESIZE_BOTTOM_LEFT:
221 pointer = WLSC_POINTER_BOTTOM_LEFT;
223 case WL_SHELL_RESIZE_RIGHT:
224 pointer = WLSC_POINTER_RIGHT;
226 case WL_SHELL_RESIZE_TOP_RIGHT:
227 pointer = WLSC_POINTER_TOP_RIGHT;
229 case WL_SHELL_RESIZE_BOTTOM_RIGHT:
230 pointer = WLSC_POINTER_BOTTOM_RIGHT;
234 if (wl_input_device_update_grab(&wd->input_device,
235 &resize->grab, surface, time) < 0)
238 wlsc_input_device_set_pointer_image(wd, pointer);
239 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
243 shell_set_toplevel(struct wl_client *client,
244 struct wl_shell *wl_shell,
245 struct wl_surface *surface)
248 struct wlsc_surface *es = (struct wlsc_surface *) surface;
249 struct wlsc_compositor *ec = es->compositor;
251 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN) {
254 } else if (es->map_type == WLSC_SURFACE_MAP_UNMAPPED) {
255 es->x = 10 + random() % 400;
256 es->y = 10 + random() % 400;
257 /* assign to first output */
258 es->output = container_of(ec->output_list.next,
259 struct wlsc_output, link);
262 wlsc_surface_damage(es);
263 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
264 es->fullscreen_output = NULL;
268 shell_set_transient(struct wl_client *client,
269 struct wl_shell *wl_shell,
270 struct wl_surface *surface,
271 struct wl_surface *parent,
272 int x, int y, uint32_t flags)
274 struct wlsc_surface *es = (struct wlsc_surface *) surface;
275 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
277 /* assign to parents output */
278 es->output = pes->output;
283 wlsc_surface_damage(es);
284 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
288 shell_set_fullscreen(struct wl_client *client,
289 struct wl_shell *wl_shell,
290 struct wl_surface *surface)
293 struct wlsc_surface *es = (struct wlsc_surface *) surface;
294 struct wlsc_output *output;
296 /* FIXME: Fullscreen on first output */
297 /* FIXME: Handle output going away */
298 output = container_of(es->compositor->output_list.next,
299 struct wlsc_output, link);
304 es->x = (output->current->width - es->width) / 2;
305 es->y = (output->current->height - es->height) / 2;
306 es->fullscreen_output = output;
307 wlsc_surface_damage(es);
308 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
312 destroy_drag(struct wl_resource *resource, struct wl_client *client)
314 struct wl_drag *drag =
315 container_of(resource, struct wl_drag, resource);
317 wl_list_remove(&drag->drag_focus_listener.link);
318 if (drag->grab.input_device)
319 wl_input_device_end_grab(drag->grab.input_device,
320 wlsc_compositor_get_time());
327 wl_drag_set_pointer_focus(struct wl_drag *drag,
328 struct wl_surface *surface, uint32_t time,
329 int32_t x, int32_t y, int32_t sx, int32_t sy)
333 if (drag->drag_focus == surface)
336 if (drag->drag_focus &&
337 (!surface || drag->drag_focus->client != surface->client))
338 wl_client_post_event(drag->drag_focus->client,
339 &drag->drag_offer.object,
340 WL_DRAG_OFFER_POINTER_FOCUS,
341 time, NULL, 0, 0, 0, 0);
344 (!drag->drag_focus ||
345 drag->drag_focus->client != surface->client)) {
346 wl_client_post_global(surface->client,
347 &drag->drag_offer.object);
349 end = drag->types.data + drag->types.size;
350 for (p = drag->types.data; p < end; p++)
351 wl_client_post_event(surface->client,
352 &drag->drag_offer.object,
353 WL_DRAG_OFFER_OFFER, *p);
357 wl_client_post_event(surface->client,
358 &drag->drag_offer.object,
359 WL_DRAG_OFFER_POINTER_FOCUS,
365 drag->drag_focus = surface;
366 drag->pointer_focus_time = time;
369 wl_list_remove(&drag->drag_focus_listener.link);
371 wl_list_insert(surface->resource.destroy_listener_list.prev,
372 &drag->drag_focus_listener.link);
376 drag_offer_accept(struct wl_client *client,
377 struct wl_drag_offer *offer, uint32_t time, const char *type)
379 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
382 /* If the client responds to drag pointer_focus or motion
383 * events after the pointer has left the surface, we just
384 * discard the accept requests. The drag source just won't
385 * get the corresponding 'target' events and eventually the
386 * next surface/root will start sending events. */
387 if (time < drag->pointer_focus_time)
390 drag->target = client;
392 end = drag->types.data + drag->types.size;
393 for (p = drag->types.data; p < end; p++)
394 if (type && strcmp(*p, type) == 0)
397 wl_client_post_event(drag->source->client, &drag->resource.object,
398 WL_DRAG_TARGET, drag->type);
402 drag_offer_receive(struct wl_client *client,
403 struct wl_drag_offer *offer, int fd)
405 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
407 wl_client_post_event(drag->source->client, &drag->resource.object,
413 drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
415 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
417 wl_client_post_event(drag->source->client, &drag->resource.object,
421 static const struct wl_drag_offer_interface drag_offer_interface = {
428 drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
432 p = wl_array_add(&drag->types, sizeof *p);
436 wl_client_post_no_memory(client);
440 drag_grab_motion(struct wl_grab *grab,
441 uint32_t time, int32_t x, int32_t y)
443 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
444 struct wlsc_surface *es;
447 es = pick_surface(grab->input_device, &sx, &sy);
448 wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy);
450 wl_client_post_event(es->surface.client,
451 &drag->drag_offer.object,
452 WL_DRAG_OFFER_MOTION,
457 drag_grab_button(struct wl_grab *grab,
458 uint32_t time, int32_t button, int32_t state)
463 drag_grab_end(struct wl_grab *grab, uint32_t time)
465 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
466 struct wlsc_surface *es;
467 struct wl_input_device *device = grab->input_device;
471 wl_client_post_event(drag->target,
472 &drag->drag_offer.object,
475 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
477 es = pick_surface(grab->input_device, &sx, &sy);
478 wl_input_device_set_pointer_focus(device,
480 device->x, device->y, sx, sy);
483 static const struct wl_grab_interface drag_grab_interface = {
490 drag_activate(struct wl_client *client,
491 struct wl_drag *drag,
492 struct wl_surface *surface,
493 struct wl_input_device *device, uint32_t time)
495 struct wl_display *display = wl_client_get_display (client);
496 struct wlsc_surface *target;
499 if (wl_input_device_update_grab(device,
500 &drag->grab, surface, time) < 0)
503 drag->grab.interface = &drag_grab_interface;
505 drag->source = surface;
507 drag->drag_offer.object.interface = &wl_drag_offer_interface;
508 drag->drag_offer.object.implementation =
509 (void (**)(void)) &drag_offer_interface;
511 wl_display_add_object(display, &drag->drag_offer.object);
513 target = pick_surface(device, &sx, &sy);
514 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
515 wl_drag_set_pointer_focus(drag, &target->surface, time,
516 device->x, device->y, sx, sy);
520 drag_destroy(struct wl_client *client, struct wl_drag *drag)
522 wl_resource_destroy(&drag->resource, client,
523 wlsc_compositor_get_time());
526 static const struct wl_drag_interface drag_interface = {
533 drag_handle_surface_destroy(struct wl_listener *listener,
534 struct wl_resource *resource, uint32_t time)
536 struct wl_drag *drag =
537 container_of(listener, struct wl_drag, drag_focus_listener);
538 struct wl_surface *surface = (struct wl_surface *) resource;
540 if (drag->drag_focus == surface)
541 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
545 shell_create_drag(struct wl_client *client,
546 struct wl_shell *shell, uint32_t id)
548 struct wl_drag *drag;
550 drag = malloc(sizeof *drag);
552 wl_client_post_no_memory(client);
556 memset(drag, 0, sizeof *drag);
557 drag->resource.object.id = id;
558 drag->resource.object.interface = &wl_drag_interface;
559 drag->resource.object.implementation =
560 (void (**)(void)) &drag_interface;
562 drag->resource.destroy = destroy_drag;
564 drag->drag_focus_listener.func = drag_handle_surface_destroy;
565 wl_list_init(&drag->drag_focus_listener.link);
567 wl_client_add_resource(client, &drag->resource);
571 wlsc_selection_set_focus(struct wlsc_shell *shell,
572 struct wl_selection *selection,
573 struct wl_surface *surface, uint32_t time)
577 if (selection->selection_focus == surface)
580 if (selection->selection_focus != NULL)
581 wl_client_post_event(selection->selection_focus->client,
582 &selection->selection_offer.object,
583 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
587 wl_client_post_global(surface->client,
588 &selection->selection_offer.object);
590 end = selection->types.data + selection->types.size;
591 for (p = selection->types.data; p < end; p++)
592 wl_client_post_event(surface->client,
593 &selection->selection_offer.object,
594 WL_SELECTION_OFFER_OFFER, *p);
596 wl_list_remove(&selection->selection_focus_listener.link);
597 wl_list_insert(surface->resource.destroy_listener_list.prev,
598 &selection->selection_focus_listener.link);
600 wl_client_post_event(surface->client,
601 &selection->selection_offer.object,
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_selection_offer *offer,
617 const char *mime_type, int fd)
619 struct wl_selection *selection =
620 container_of(offer, struct wl_selection, selection_offer);
622 wl_client_post_event(selection->client,
623 &selection->resource.object,
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_selection *selection, const char *type)
638 p = wl_array_add(&selection->types, sizeof *p);
642 wl_client_post_no_memory(client);
646 selection_activate(struct wl_client *client,
647 struct wl_selection *selection,
648 struct wl_input_device *device, uint32_t time)
650 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
651 struct wl_display *display = wl_client_get_display (client);
652 struct wlsc_compositor *compositor =
653 (struct wlsc_compositor *) device->compositor;
655 selection->input_device = device;
657 selection->selection_offer.object.interface =
658 &wl_selection_offer_interface;
659 selection->selection_offer.object.implementation =
660 (void (**)(void)) &selection_offer_interface;
662 wl_display_add_object(display, &selection->selection_offer.object);
665 wl_client_post_event(wd->selection->client,
666 &wd->selection->resource.object,
667 WL_SELECTION_CANCELLED);
669 wd->selection = selection;
671 wlsc_selection_set_focus(compositor->shell,
672 selection, device->keyboard_focus, time);
676 selection_destroy(struct wl_client *client, struct wl_selection *selection)
678 wl_resource_destroy(&selection->resource, client,
679 wlsc_compositor_get_time());
682 static const struct wl_selection_interface selection_interface = {
689 destroy_selection(struct wl_resource *resource, struct wl_client *client)
691 struct wl_selection *selection =
692 container_of(resource, struct wl_selection, resource);
693 struct wlsc_input_device *wd =
694 (struct wlsc_input_device *) selection->input_device;
695 struct wlsc_compositor *compositor =
696 (struct wlsc_compositor *) wd->input_device.compositor;
698 if (wd && wd->selection == selection) {
699 wd->selection = NULL;
700 wlsc_selection_set_focus(compositor->shell,
702 wlsc_compositor_get_time());
705 wl_list_remove(&selection->selection_focus_listener.link);
710 selection_handle_surface_destroy(struct wl_listener *listener,
711 struct wl_resource *resource, uint32_t time)
716 shell_create_selection(struct wl_client *client,
717 struct wl_shell *shell, uint32_t id)
719 struct wl_selection *selection;
721 selection = malloc(sizeof *selection);
722 if (selection == NULL) {
723 wl_client_post_no_memory(client);
727 memset(selection, 0, sizeof *selection);
728 selection->resource.object.id = id;
729 selection->resource.object.interface = &wl_selection_interface;
730 selection->resource.object.implementation =
731 (void (**)(void)) &selection_interface;
733 selection->client = client;
734 selection->resource.destroy = destroy_selection;
735 selection->selection_focus = NULL;
737 selection->selection_focus_listener.func =
738 selection_handle_surface_destroy;
739 wl_list_init(&selection->selection_focus_listener.link);
741 wl_client_add_resource(client, &selection->resource);
744 const static struct wl_shell_interface shell_interface = {
748 shell_create_selection,
755 move_binding(struct wl_input_device *device, uint32_t time,
756 uint32_t key, uint32_t button, uint32_t state, void *data)
758 struct wl_shell *shell = data;
759 struct wlsc_surface *surface =
760 (struct wlsc_surface *) device->pointer_focus;
765 shell_move(NULL, shell, &surface->surface, device, time);
769 resize_binding(struct wl_input_device *device, uint32_t time,
770 uint32_t key, uint32_t button, uint32_t state, void *data)
772 struct wl_shell *shell = 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, shell, &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 wl_display_add_object(ec->wl_display, &shell->object);
834 if (wl_display_add_global(ec->wl_display, &shell->object, NULL))
837 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
838 move_binding, shell);
839 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
840 resize_binding, shell);
842 ec->shell = &shell->shell;