blob: d50a3d695f1cc5a7843716e511111b511b59c57f [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;
32 int32_t dx, dy;
33};
34
35static void
36move_grab_motion(struct wl_grab *grab,
37 uint32_t time, int32_t x, int32_t y)
38{
39 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
40 struct wlsc_surface *es =
41 (struct wlsc_surface *) grab->input_device->pointer_focus;
42
43 es->x = x + move->dx;
44 es->y = y + move->dy;
45 wlsc_surface_update_matrix(es);
46}
47
48static void
49move_grab_button(struct wl_grab *grab,
50 uint32_t time, int32_t button, int32_t state)
51{
52}
53
54static void
55move_grab_end(struct wl_grab *grab, uint32_t time)
56{
57 free(grab);
58}
59
60static const struct wl_grab_interface move_grab_interface = {
61 move_grab_motion,
62 move_grab_button,
63 move_grab_end
64};
65
66void
67shell_move(struct wl_client *client, struct wl_shell *shell,
68 struct wl_surface *surface,
69 struct wl_input_device *device, uint32_t time)
70{
71 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
72 struct wlsc_surface *es = (struct wlsc_surface *) surface;
73 struct wlsc_move_grab *move;
74
75 move = malloc(sizeof *move);
76 if (!move) {
77 wl_client_post_no_memory(client);
78 return;
79 }
80
81 move->grab.interface = &move_grab_interface;
82 move->dx = es->x - wd->input_device.grab_x;
83 move->dy = es->y - wd->input_device.grab_y;
84
85 if (wl_input_device_update_grab(&wd->input_device,
86 &move->grab, surface, time) < 0)
87 return;
88
89 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
90}
91
92struct wlsc_resize_grab {
93 struct wl_grab grab;
94 uint32_t edges;
95 int32_t dx, dy, width, height;
96};
97
98static void
99resize_grab_motion(struct wl_grab *grab,
100 uint32_t time, int32_t x, int32_t y)
101{
102 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
103 struct wl_input_device *device = grab->input_device;
104 struct wlsc_compositor *ec =
105 (struct wlsc_compositor *) device->compositor;
106 struct wl_surface *surface = device->pointer_focus;
107 int32_t width, height;
108
109 if (resize->edges & WL_GRAB_RESIZE_LEFT) {
110 width = device->grab_x - x + resize->width;
111 } else if (resize->edges & WL_GRAB_RESIZE_RIGHT) {
112 width = x - device->grab_x + resize->width;
113 } else {
114 width = resize->width;
115 }
116
117 if (resize->edges & WL_GRAB_RESIZE_TOP) {
118 height = device->grab_y - y + resize->height;
119 } else if (resize->edges & WL_GRAB_RESIZE_BOTTOM) {
120 height = y - device->grab_y + resize->height;
121 } else {
122 height = resize->height;
123 }
124
125 wl_client_post_event(surface->client, &ec->shell.object,
126 WL_SHELL_CONFIGURE, time, resize->edges,
127 surface, width, height);
128}
129
130static void
131resize_grab_button(struct wl_grab *grab,
132 uint32_t time, int32_t button, int32_t state)
133{
134}
135
136static void
137resize_grab_end(struct wl_grab *grab, uint32_t time)
138{
139 free(grab);
140}
141
142static const struct wl_grab_interface resize_grab_interface = {
143 resize_grab_motion,
144 resize_grab_button,
145 resize_grab_end
146};
147
148void
149shell_resize(struct wl_client *client, struct wl_shell *shell,
150 struct wl_surface *surface,
151 struct wl_input_device *device, uint32_t time, uint32_t edges)
152{
153 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
154 struct wlsc_resize_grab *resize;
155 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
156 struct wlsc_surface *es = (struct wlsc_surface *) surface;
157
158 resize = malloc(sizeof *resize);
159 if (!resize) {
160 wl_client_post_no_memory(client);
161 return;
162 }
163
164 resize->grab.interface = &resize_grab_interface;
165 resize->edges = edges;
166 resize->dx = es->x - wd->input_device.grab_x;
167 resize->dy = es->y - wd->input_device.grab_y;
168 resize->width = es->width;
169 resize->height = es->height;
170
171 if (edges == 0 || edges > 15 ||
172 (edges & 3) == 3 || (edges & 12) == 12)
173 return;
174
175 switch (edges) {
176 case WL_GRAB_RESIZE_TOP:
177 pointer = WLSC_POINTER_TOP;
178 break;
179 case WL_GRAB_RESIZE_BOTTOM:
180 pointer = WLSC_POINTER_BOTTOM;
181 break;
182 case WL_GRAB_RESIZE_LEFT:
183 pointer = WLSC_POINTER_LEFT;
184 break;
185 case WL_GRAB_RESIZE_TOP_LEFT:
186 pointer = WLSC_POINTER_TOP_LEFT;
187 break;
188 case WL_GRAB_RESIZE_BOTTOM_LEFT:
189 pointer = WLSC_POINTER_BOTTOM_LEFT;
190 break;
191 case WL_GRAB_RESIZE_RIGHT:
192 pointer = WLSC_POINTER_RIGHT;
193 break;
194 case WL_GRAB_RESIZE_TOP_RIGHT:
195 pointer = WLSC_POINTER_TOP_RIGHT;
196 break;
197 case WL_GRAB_RESIZE_BOTTOM_RIGHT:
198 pointer = WLSC_POINTER_BOTTOM_RIGHT;
199 break;
200 }
201
202 if (wl_input_device_update_grab(&wd->input_device,
203 &resize->grab, surface, time) < 0)
204 return;
205
206 wlsc_input_device_set_pointer_image(wd, pointer);
207}
208
209static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500210destroy_drag(struct wl_resource *resource, struct wl_client *client)
211{
212 struct wl_drag *drag =
213 container_of(resource, struct wl_drag, resource);
214
215 wl_list_remove(&drag->drag_focus_listener.link);
216 if (drag->grab.input_device)
217 wl_input_device_end_grab(drag->grab.input_device, get_time());
218
219 free(drag);
220}
221
222
223static void
224wl_drag_set_pointer_focus(struct wl_drag *drag,
225 struct wl_surface *surface, uint32_t time,
226 int32_t x, int32_t y, int32_t sx, int32_t sy)
227{
228 char **p, **end;
229
230 if (drag->drag_focus == surface)
231 return;
232
233 if (drag->drag_focus &&
234 (!surface || drag->drag_focus->client != surface->client))
235 wl_client_post_event(drag->drag_focus->client,
236 &drag->drag_offer.object,
237 WL_DRAG_OFFER_POINTER_FOCUS,
238 time, NULL, 0, 0, 0, 0);
239
240 if (surface &&
241 (!drag->drag_focus ||
242 drag->drag_focus->client != surface->client)) {
243 wl_client_post_global(surface->client,
244 &drag->drag_offer.object);
245
246 end = drag->types.data + drag->types.size;
247 for (p = drag->types.data; p < end; p++)
248 wl_client_post_event(surface->client,
249 &drag->drag_offer.object,
250 WL_DRAG_OFFER_OFFER, *p);
251 }
252
253 if (surface) {
254 wl_client_post_event(surface->client,
255 &drag->drag_offer.object,
256 WL_DRAG_OFFER_POINTER_FOCUS,
257 time, surface,
258 x, y, sx, sy);
259
260 }
261
262 drag->drag_focus = surface;
263 drag->pointer_focus_time = time;
264 drag->target = NULL;
265
266 wl_list_remove(&drag->drag_focus_listener.link);
267 if (surface)
268 wl_list_insert(surface->destroy_listener_list.prev,
269 &drag->drag_focus_listener.link);
270}
271
272static void
273drag_offer_accept(struct wl_client *client,
274 struct wl_drag_offer *offer, uint32_t time, const char *type)
275{
276 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
277 char **p, **end;
278
279 /* If the client responds to drag pointer_focus or motion
280 * events after the pointer has left the surface, we just
281 * discard the accept requests. The drag source just won't
282 * get the corresponding 'target' events and eventually the
283 * next surface/root will start sending events. */
284 if (time < drag->pointer_focus_time)
285 return;
286
287 drag->target = client;
288 drag->type = NULL;
289 end = drag->types.data + drag->types.size;
290 for (p = drag->types.data; p < end; p++)
291 if (type && strcmp(*p, type) == 0)
292 drag->type = *p;
293
294 wl_client_post_event(drag->source->client, &drag->resource.object,
295 WL_DRAG_TARGET, drag->type);
296}
297
298static void
299drag_offer_receive(struct wl_client *client,
300 struct wl_drag_offer *offer, int fd)
301{
302 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
303
304 wl_client_post_event(drag->source->client, &drag->resource.object,
305 WL_DRAG_FINISH, fd);
306 close(fd);
307}
308
309static void
310drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
311{
312 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
313
314 wl_client_post_event(drag->source->client, &drag->resource.object,
315 WL_DRAG_REJECT);
316}
317
318static const struct wl_drag_offer_interface drag_offer_interface = {
319 drag_offer_accept,
320 drag_offer_receive,
321 drag_offer_reject
322};
323
324static void
325drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
326{
327 char **p;
328
329 p = wl_array_add(&drag->types, sizeof *p);
330 if (p)
331 *p = strdup(type);
332 if (!p || !*p)
333 wl_client_post_no_memory(client);
334}
335
336static void
337drag_grab_motion(struct wl_grab *grab,
338 uint32_t time, int32_t x, int32_t y)
339{
340 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
341 struct wlsc_surface *es;
342 int32_t sx, sy;
343
344 es = pick_surface(grab->input_device, &sx, &sy);
345 wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy);
346 if (es)
347 wl_client_post_event(es->surface.client,
348 &drag->drag_offer.object,
349 WL_DRAG_OFFER_MOTION,
350 time, x, y, sx, sy);
351}
352
353static void
354drag_grab_button(struct wl_grab *grab,
355 uint32_t time, int32_t button, int32_t state)
356{
357}
358
359static void
360drag_grab_end(struct wl_grab *grab, uint32_t time)
361{
362 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
363
364 if (drag->target)
365 wl_client_post_event(drag->target,
366 &drag->drag_offer.object,
367 WL_DRAG_OFFER_DROP);
368
369 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
370}
371
372static const struct wl_grab_interface drag_grab_interface = {
373 drag_grab_motion,
374 drag_grab_button,
375 drag_grab_end
376};
377
378static void
379drag_activate(struct wl_client *client,
380 struct wl_drag *drag,
381 struct wl_surface *surface,
382 struct wl_input_device *device, uint32_t time)
383{
384 struct wl_display *display = wl_client_get_display (client);
385 struct wlsc_surface *target;
386 int32_t sx, sy;
387
388 if (wl_input_device_update_grab(device,
389 &drag->grab, surface, time) < 0)
390 return;
391
392 drag->grab.interface = &drag_grab_interface;
393
394 drag->source = surface;
395
396 drag->drag_offer.object.interface = &wl_drag_offer_interface;
397 drag->drag_offer.object.implementation =
398 (void (**)(void)) &drag_offer_interface;
399
400 wl_display_add_object(display, &drag->drag_offer.object);
401
402 target = pick_surface(device, &sx, &sy);
403 wl_drag_set_pointer_focus(drag, &target->surface, time,
404 device->x, device->y, sx, sy);
405}
406
407static void
408drag_destroy(struct wl_client *client, struct wl_drag *drag)
409{
410 wl_resource_destroy(&drag->resource, client);
411}
412
413static const struct wl_drag_interface drag_interface = {
414 drag_offer,
415 drag_activate,
416 drag_destroy,
417};
418
419static void
420drag_handle_surface_destroy(struct wl_listener *listener,
421 struct wl_surface *surface, uint32_t time)
422{
423 struct wl_drag *drag =
424 container_of(listener, struct wl_drag, drag_focus_listener);
425
426 if (drag->drag_focus == surface)
427 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
428}
429
430static void
431shell_create_drag(struct wl_client *client,
432 struct wl_shell *shell, uint32_t id)
433{
434 struct wl_drag *drag;
435
436 drag = malloc(sizeof *drag);
437 if (drag == NULL) {
438 wl_client_post_no_memory(client);
439 return;
440 }
441
442 memset(drag, 0, sizeof *drag);
443 drag->resource.object.id = id;
444 drag->resource.object.interface = &wl_drag_interface;
445 drag->resource.object.implementation =
446 (void (**)(void)) &drag_interface;
447
448 drag->resource.destroy = destroy_drag;
449
450 drag->drag_focus_listener.func = drag_handle_surface_destroy;
451 wl_list_init(&drag->drag_focus_listener.link);
452
453 wl_client_add_resource(client, &drag->resource);
454}
455
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500456void
457wlsc_selection_set_focus(struct wl_selection *selection,
458 struct wl_surface *surface, uint32_t time)
459{
460 char **p, **end;
461
462 if (selection->selection_focus == surface)
463 return;
464
465 if (selection->selection_focus != NULL)
466 wl_client_post_event(selection->selection_focus->client,
467 &selection->selection_offer.object,
468 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
469 NULL);
470
471 if (surface) {
472 wl_client_post_global(surface->client,
473 &selection->selection_offer.object);
474
475 end = selection->types.data + selection->types.size;
476 for (p = selection->types.data; p < end; p++)
477 wl_client_post_event(surface->client,
478 &selection->selection_offer.object,
479 WL_SELECTION_OFFER_OFFER, *p);
480
481 wl_list_remove(&selection->selection_focus_listener.link);
482 wl_list_insert(surface->destroy_listener_list.prev,
483 &selection->selection_focus_listener.link);
484
485 wl_client_post_event(surface->client,
486 &selection->selection_offer.object,
487 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
488 selection->input_device);
489 }
490
491 selection->selection_focus = surface;
492
493 wl_list_remove(&selection->selection_focus_listener.link);
494 if (surface)
495 wl_list_insert(surface->destroy_listener_list.prev,
496 &selection->selection_focus_listener.link);
497}
498
499static void
500selection_offer_receive(struct wl_client *client,
501 struct wl_selection_offer *offer,
502 const char *mime_type, int fd)
503{
504 struct wl_selection *selection =
505 container_of(offer, struct wl_selection, selection_offer);
506
507 wl_client_post_event(selection->client,
508 &selection->resource.object,
509 WL_SELECTION_SEND, mime_type, fd);
510 close(fd);
511}
512
513static const struct wl_selection_offer_interface selection_offer_interface = {
514 selection_offer_receive
515};
516
517static void
518selection_offer(struct wl_client *client,
519 struct wl_selection *selection, const char *type)
520{
521 char **p;
522
523 p = wl_array_add(&selection->types, sizeof *p);
524 if (p)
525 *p = strdup(type);
526 if (!p || !*p)
527 wl_client_post_no_memory(client);
528}
529
530static void
531selection_activate(struct wl_client *client,
532 struct wl_selection *selection,
533 struct wl_input_device *device, uint32_t time)
534{
535 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
536 struct wl_display *display = wl_client_get_display (client);
537
538 selection->input_device = device;
539
540 selection->selection_offer.object.interface =
541 &wl_selection_offer_interface;
542 selection->selection_offer.object.implementation =
543 (void (**)(void)) &selection_offer_interface;
544
545 wl_display_add_object(display, &selection->selection_offer.object);
546
547 if (wd->selection) {
548 wl_client_post_event(wd->selection->client,
549 &wd->selection->resource.object,
550 WL_SELECTION_CANCELLED);
551 }
552 wd->selection = selection;
553
554 wlsc_selection_set_focus(selection, device->keyboard_focus, time);
555}
556
557static void
558selection_destroy(struct wl_client *client, struct wl_selection *selection)
559{
560 wl_resource_destroy(&selection->resource, client);
561}
562
563static const struct wl_selection_interface selection_interface = {
564 selection_offer,
565 selection_activate,
566 selection_destroy
567};
568
569static void
570destroy_selection(struct wl_resource *resource, struct wl_client *client)
571{
572 struct wl_selection *selection =
573 container_of(resource, struct wl_selection, resource);
574 struct wlsc_input_device *wd =
575 (struct wlsc_input_device *) selection->input_device;
576
577 if (wd && wd->selection == selection) {
578 wd->selection = NULL;
579 wlsc_selection_set_focus(selection, NULL, get_time());
580 }
581
582 wl_list_remove(&selection->selection_focus_listener.link);
583 free(selection);
584}
585
586static void
587selection_handle_surface_destroy(struct wl_listener *listener,
588 struct wl_surface *surface, uint32_t time)
589{
590}
591
592static void
593shell_create_selection(struct wl_client *client,
594 struct wl_shell *shell, uint32_t id)
595{
596 struct wl_selection *selection;
597
598 selection = malloc(sizeof *selection);
599 if (selection == NULL) {
600 wl_client_post_no_memory(client);
601 return;
602 }
603
604 memset(selection, 0, sizeof *selection);
605 selection->resource.object.id = id;
606 selection->resource.object.interface = &wl_selection_interface;
607 selection->resource.object.implementation =
608 (void (**)(void)) &selection_interface;
609
610 selection->client = client;
611 selection->resource.destroy = destroy_selection;
612 selection->selection_focus = NULL;
613
614 selection->selection_focus_listener.func =
615 selection_handle_surface_destroy;
616 wl_list_init(&selection->selection_focus_listener.link);
617
618 wl_client_add_resource(client, &selection->resource);
619}
620
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500621const static struct wl_shell_interface shell_interface = {
622 shell_move,
623 shell_resize,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500624 shell_create_drag,
625 shell_create_selection
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500626};
627
628int
629wlsc_shell_init(struct wlsc_compositor *ec)
630{
631 struct wl_shell *shell = &ec->shell;
632
633 shell->object.interface = &wl_shell_interface;
634 shell->object.implementation = (void (**)(void)) &shell_interface;
635 wl_display_add_object(ec->wl_display, &shell->object);
636 if (wl_display_add_global(ec->wl_display, &shell->object, NULL))
637 return -1;
638
639 return 0;
640}