blob: 91af25686233025b2bd2cfd775ab52553a53ccbc [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øgsberga691aee2011-06-23 21:43:50 -040049 wlsc_surface_configure(es, x + move->dx, y + move->dy,
50 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050051}
52
53static void
54move_grab_button(struct wl_grab *grab,
55 uint32_t time, int32_t button, int32_t state)
56{
57}
58
59static void
60move_grab_end(struct wl_grab *grab, uint32_t time)
61{
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050062 struct wlsc_surface *es;
63 struct wl_input_device *device = grab->input_device;
64 int32_t sx, sy;
65
66 es = pick_surface(grab->input_device, &sx, &sy);
67 wl_input_device_set_pointer_focus(device,
68 &es->surface, time,
69 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050070 free(grab);
71}
72
73static const struct wl_grab_interface move_grab_interface = {
74 move_grab_motion,
75 move_grab_button,
76 move_grab_end
77};
78
Kristian Høgsberg07937562011-04-12 17:25:42 -040079static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050080shell_move(struct wl_client *client, struct wl_shell *shell,
81 struct wl_surface *surface,
82 struct wl_input_device *device, uint32_t time)
83{
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;
87
Kristian Høgsberg0ce24572011-01-28 15:18:33 -050088 /* FIXME: Reject if fullscreen */
89
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050090 move = malloc(sizeof *move);
91 if (!move) {
92 wl_client_post_no_memory(client);
93 return;
94 }
95
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;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050099 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500100
101 if (wl_input_device_update_grab(&wd->input_device,
102 &move->grab, surface, time) < 0)
103 return;
104
105 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500106 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500107}
108
109struct wlsc_resize_grab {
110 struct wl_grab grab;
111 uint32_t edges;
112 int32_t dx, dy, width, height;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500113 struct wlsc_surface *surface;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400114 struct wl_shell *shell;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500115};
116
117static void
118resize_grab_motion(struct wl_grab *grab,
119 uint32_t time, int32_t x, int32_t y)
120{
121 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
122 struct wl_input_device *device = grab->input_device;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500123 struct wl_surface *surface = &resize->surface->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500124 int32_t width, height;
125
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500126 if (resize->edges & WL_SHELL_RESIZE_LEFT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500127 width = device->grab_x - x + resize->width;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500128 } else if (resize->edges & WL_SHELL_RESIZE_RIGHT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500129 width = x - device->grab_x + resize->width;
130 } else {
131 width = resize->width;
132 }
133
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500134 if (resize->edges & WL_SHELL_RESIZE_TOP) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500135 height = device->grab_y - y + resize->height;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500136 } else if (resize->edges & WL_SHELL_RESIZE_BOTTOM) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500137 height = y - device->grab_y + resize->height;
138 } else {
139 height = resize->height;
140 }
141
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400142 wl_client_post_event(surface->client, &resize->shell->object,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500143 WL_SHELL_CONFIGURE, time, resize->edges,
144 surface, width, height);
145}
146
147static void
148resize_grab_button(struct wl_grab *grab,
149 uint32_t time, int32_t button, int32_t state)
150{
151}
152
153static void
154resize_grab_end(struct wl_grab *grab, uint32_t time)
155{
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500156 struct wlsc_surface *es;
157 struct wl_input_device *device = grab->input_device;
158 int32_t sx, sy;
159
160 es = pick_surface(grab->input_device, &sx, &sy);
161 wl_input_device_set_pointer_focus(device,
162 &es->surface, time,
163 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500164 free(grab);
165}
166
167static const struct wl_grab_interface resize_grab_interface = {
168 resize_grab_motion,
169 resize_grab_button,
170 resize_grab_end
171};
172
Kristian Høgsberg07937562011-04-12 17:25:42 -0400173static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500174shell_resize(struct wl_client *client, struct wl_shell *shell,
175 struct wl_surface *surface,
176 struct wl_input_device *device, uint32_t time, uint32_t edges)
177{
178 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
179 struct wlsc_resize_grab *resize;
180 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
181 struct wlsc_surface *es = (struct wlsc_surface *) surface;
182
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500183 /* FIXME: Reject if fullscreen */
184
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500185 resize = malloc(sizeof *resize);
186 if (!resize) {
187 wl_client_post_no_memory(client);
188 return;
189 }
190
191 resize->grab.interface = &resize_grab_interface;
192 resize->edges = edges;
193 resize->dx = es->x - wd->input_device.grab_x;
194 resize->dy = es->y - wd->input_device.grab_y;
195 resize->width = es->width;
196 resize->height = es->height;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500197 resize->surface = es;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400198 resize->shell = shell;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500199
200 if (edges == 0 || edges > 15 ||
201 (edges & 3) == 3 || (edges & 12) == 12)
202 return;
203
204 switch (edges) {
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500205 case WL_SHELL_RESIZE_TOP:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500206 pointer = WLSC_POINTER_TOP;
207 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500208 case WL_SHELL_RESIZE_BOTTOM:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500209 pointer = WLSC_POINTER_BOTTOM;
210 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500211 case WL_SHELL_RESIZE_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500212 pointer = WLSC_POINTER_LEFT;
213 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500214 case WL_SHELL_RESIZE_TOP_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500215 pointer = WLSC_POINTER_TOP_LEFT;
216 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500217 case WL_SHELL_RESIZE_BOTTOM_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500218 pointer = WLSC_POINTER_BOTTOM_LEFT;
219 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500220 case WL_SHELL_RESIZE_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500221 pointer = WLSC_POINTER_RIGHT;
222 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500223 case WL_SHELL_RESIZE_TOP_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500224 pointer = WLSC_POINTER_TOP_RIGHT;
225 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500226 case WL_SHELL_RESIZE_BOTTOM_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500227 pointer = WLSC_POINTER_BOTTOM_RIGHT;
228 break;
229 }
230
231 if (wl_input_device_update_grab(&wd->input_device,
232 &resize->grab, surface, time) < 0)
233 return;
234
235 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500236 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500237}
238
239static void
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400240shell_set_toplevel(struct wl_client *client,
241 struct wl_shell *wl_shell,
242 struct wl_surface *surface)
243
244{
245 struct wlsc_surface *es = (struct wlsc_surface *) surface;
246 struct wlsc_compositor *ec = es->compositor;
247
248 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN) {
249 es->x = es->saved_x;
250 es->y = es->saved_y;
251 } else if (es->map_type == WLSC_SURFACE_MAP_UNMAPPED) {
252 es->x = 10 + random() % 400;
253 es->y = 10 + random() % 400;
254 /* assign to first output */
255 es->output = container_of(ec->output_list.next,
256 struct wlsc_output, link);
257 }
258
259 wlsc_surface_damage(es);
260 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
261 es->fullscreen_output = NULL;
262}
263
264static void
265shell_set_transient(struct wl_client *client,
266 struct wl_shell *wl_shell,
267 struct wl_surface *surface,
268 struct wl_surface *parent,
269 int x, int y, uint32_t flags)
270{
271 struct wlsc_surface *es = (struct wlsc_surface *) surface;
272 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
273
274 /* assign to parents output */
275 es->output = pes->output;
276
277 es->x = pes->x + x;
278 es->y = pes->y + y;
279
280 wlsc_surface_damage(es);
281 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
282}
283
284static void
285shell_set_fullscreen(struct wl_client *client,
286 struct wl_shell *wl_shell,
287 struct wl_surface *surface)
288
289{
290 struct wlsc_surface *es = (struct wlsc_surface *) surface;
291 struct wlsc_output *output;
292
293 /* FIXME: Fullscreen on first output */
294 /* FIXME: Handle output going away */
295 output = container_of(es->compositor->output_list.next,
296 struct wlsc_output, link);
297 es->output = output;
298
299 es->saved_x = es->x;
300 es->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400301 es->x = (output->current->width - es->width) / 2;
302 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400303 es->fullscreen_output = output;
304 wlsc_surface_damage(es);
305 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
306}
307
308static void
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500309destroy_drag(struct wl_resource *resource, struct wl_client *client)
310{
311 struct wl_drag *drag =
312 container_of(resource, struct wl_drag, resource);
313
314 wl_list_remove(&drag->drag_focus_listener.link);
315 if (drag->grab.input_device)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400316 wl_input_device_end_grab(drag->grab.input_device,
317 wlsc_compositor_get_time());
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500318
319 free(drag);
320}
321
322
323static void
324wl_drag_set_pointer_focus(struct wl_drag *drag,
325 struct wl_surface *surface, uint32_t time,
326 int32_t x, int32_t y, int32_t sx, int32_t sy)
327{
328 char **p, **end;
329
330 if (drag->drag_focus == surface)
331 return;
332
333 if (drag->drag_focus &&
334 (!surface || drag->drag_focus->client != surface->client))
335 wl_client_post_event(drag->drag_focus->client,
336 &drag->drag_offer.object,
337 WL_DRAG_OFFER_POINTER_FOCUS,
338 time, NULL, 0, 0, 0, 0);
339
340 if (surface &&
341 (!drag->drag_focus ||
342 drag->drag_focus->client != surface->client)) {
343 wl_client_post_global(surface->client,
344 &drag->drag_offer.object);
345
346 end = drag->types.data + drag->types.size;
347 for (p = drag->types.data; p < end; p++)
348 wl_client_post_event(surface->client,
349 &drag->drag_offer.object,
350 WL_DRAG_OFFER_OFFER, *p);
351 }
352
353 if (surface) {
354 wl_client_post_event(surface->client,
355 &drag->drag_offer.object,
356 WL_DRAG_OFFER_POINTER_FOCUS,
357 time, surface,
358 x, y, sx, sy);
359
360 }
361
362 drag->drag_focus = surface;
363 drag->pointer_focus_time = time;
364 drag->target = NULL;
365
366 wl_list_remove(&drag->drag_focus_listener.link);
367 if (surface)
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200368 wl_list_insert(surface->resource.destroy_listener_list.prev,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500369 &drag->drag_focus_listener.link);
370}
371
372static void
373drag_offer_accept(struct wl_client *client,
374 struct wl_drag_offer *offer, uint32_t time, const char *type)
375{
376 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
377 char **p, **end;
378
379 /* If the client responds to drag pointer_focus or motion
380 * events after the pointer has left the surface, we just
381 * discard the accept requests. The drag source just won't
382 * get the corresponding 'target' events and eventually the
383 * next surface/root will start sending events. */
384 if (time < drag->pointer_focus_time)
385 return;
386
387 drag->target = client;
388 drag->type = NULL;
389 end = drag->types.data + drag->types.size;
390 for (p = drag->types.data; p < end; p++)
391 if (type && strcmp(*p, type) == 0)
392 drag->type = *p;
393
394 wl_client_post_event(drag->source->client, &drag->resource.object,
395 WL_DRAG_TARGET, drag->type);
396}
397
398static void
399drag_offer_receive(struct wl_client *client,
400 struct wl_drag_offer *offer, int fd)
401{
402 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
403
404 wl_client_post_event(drag->source->client, &drag->resource.object,
405 WL_DRAG_FINISH, fd);
406 close(fd);
407}
408
409static void
410drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer)
411{
412 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
413
414 wl_client_post_event(drag->source->client, &drag->resource.object,
415 WL_DRAG_REJECT);
416}
417
418static const struct wl_drag_offer_interface drag_offer_interface = {
419 drag_offer_accept,
420 drag_offer_receive,
421 drag_offer_reject
422};
423
424static void
425drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type)
426{
427 char **p;
428
429 p = wl_array_add(&drag->types, sizeof *p);
430 if (p)
431 *p = strdup(type);
432 if (!p || !*p)
433 wl_client_post_no_memory(client);
434}
435
436static void
437drag_grab_motion(struct wl_grab *grab,
438 uint32_t time, int32_t x, int32_t y)
439{
440 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
441 struct wlsc_surface *es;
442 int32_t sx, sy;
443
444 es = pick_surface(grab->input_device, &sx, &sy);
445 wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy);
446 if (es)
447 wl_client_post_event(es->surface.client,
448 &drag->drag_offer.object,
449 WL_DRAG_OFFER_MOTION,
450 time, x, y, sx, sy);
451}
452
453static void
454drag_grab_button(struct wl_grab *grab,
455 uint32_t time, int32_t button, int32_t state)
456{
457}
458
459static void
460drag_grab_end(struct wl_grab *grab, uint32_t time)
461{
462 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500463 struct wlsc_surface *es;
464 struct wl_input_device *device = grab->input_device;
465 int32_t sx, sy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500466
467 if (drag->target)
468 wl_client_post_event(drag->target,
469 &drag->drag_offer.object,
470 WL_DRAG_OFFER_DROP);
471
472 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500473
474 es = pick_surface(grab->input_device, &sx, &sy);
475 wl_input_device_set_pointer_focus(device,
476 &es->surface, time,
477 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500478}
479
480static const struct wl_grab_interface drag_grab_interface = {
481 drag_grab_motion,
482 drag_grab_button,
483 drag_grab_end
484};
485
486static void
487drag_activate(struct wl_client *client,
488 struct wl_drag *drag,
489 struct wl_surface *surface,
490 struct wl_input_device *device, uint32_t time)
491{
492 struct wl_display *display = wl_client_get_display (client);
493 struct wlsc_surface *target;
494 int32_t sx, sy;
495
496 if (wl_input_device_update_grab(device,
497 &drag->grab, surface, time) < 0)
498 return;
499
500 drag->grab.interface = &drag_grab_interface;
501
502 drag->source = surface;
503
504 drag->drag_offer.object.interface = &wl_drag_offer_interface;
505 drag->drag_offer.object.implementation =
506 (void (**)(void)) &drag_offer_interface;
507
508 wl_display_add_object(display, &drag->drag_offer.object);
509
510 target = pick_surface(device, &sx, &sy);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500511 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500512 wl_drag_set_pointer_focus(drag, &target->surface, time,
513 device->x, device->y, sx, sy);
514}
515
516static void
517drag_destroy(struct wl_client *client, struct wl_drag *drag)
518{
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200519 wl_resource_destroy(&drag->resource, client,
520 wlsc_compositor_get_time());
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500521}
522
523static const struct wl_drag_interface drag_interface = {
524 drag_offer,
525 drag_activate,
526 drag_destroy,
527};
528
529static void
530drag_handle_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200531 struct wl_resource *resource, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500532{
533 struct wl_drag *drag =
534 container_of(listener, struct wl_drag, drag_focus_listener);
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200535 struct wl_surface *surface = (struct wl_surface *) resource;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500536
537 if (drag->drag_focus == surface)
538 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
539}
540
541static void
542shell_create_drag(struct wl_client *client,
543 struct wl_shell *shell, uint32_t id)
544{
545 struct wl_drag *drag;
546
547 drag = malloc(sizeof *drag);
548 if (drag == NULL) {
549 wl_client_post_no_memory(client);
550 return;
551 }
552
553 memset(drag, 0, sizeof *drag);
554 drag->resource.object.id = id;
555 drag->resource.object.interface = &wl_drag_interface;
556 drag->resource.object.implementation =
557 (void (**)(void)) &drag_interface;
558
559 drag->resource.destroy = destroy_drag;
560
561 drag->drag_focus_listener.func = drag_handle_surface_destroy;
562 wl_list_init(&drag->drag_focus_listener.link);
563
564 wl_client_add_resource(client, &drag->resource);
565}
566
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400567static void
568wlsc_selection_set_focus(struct wlsc_shell *shell,
569 struct wl_selection *selection,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500570 struct wl_surface *surface, uint32_t time)
571{
572 char **p, **end;
573
574 if (selection->selection_focus == surface)
575 return;
576
577 if (selection->selection_focus != NULL)
578 wl_client_post_event(selection->selection_focus->client,
579 &selection->selection_offer.object,
580 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
581 NULL);
582
583 if (surface) {
584 wl_client_post_global(surface->client,
585 &selection->selection_offer.object);
586
587 end = selection->types.data + selection->types.size;
588 for (p = selection->types.data; p < end; p++)
589 wl_client_post_event(surface->client,
590 &selection->selection_offer.object,
591 WL_SELECTION_OFFER_OFFER, *p);
592
593 wl_list_remove(&selection->selection_focus_listener.link);
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200594 wl_list_insert(surface->resource.destroy_listener_list.prev,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500595 &selection->selection_focus_listener.link);
596
597 wl_client_post_event(surface->client,
598 &selection->selection_offer.object,
599 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
600 selection->input_device);
601 }
602
603 selection->selection_focus = surface;
604
605 wl_list_remove(&selection->selection_focus_listener.link);
606 if (surface)
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200607 wl_list_insert(surface->resource.destroy_listener_list.prev,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500608 &selection->selection_focus_listener.link);
609}
610
611static void
612selection_offer_receive(struct wl_client *client,
613 struct wl_selection_offer *offer,
614 const char *mime_type, int fd)
615{
616 struct wl_selection *selection =
617 container_of(offer, struct wl_selection, selection_offer);
618
619 wl_client_post_event(selection->client,
620 &selection->resource.object,
621 WL_SELECTION_SEND, mime_type, fd);
622 close(fd);
623}
624
625static const struct wl_selection_offer_interface selection_offer_interface = {
626 selection_offer_receive
627};
628
629static void
630selection_offer(struct wl_client *client,
631 struct wl_selection *selection, const char *type)
632{
633 char **p;
634
635 p = wl_array_add(&selection->types, sizeof *p);
636 if (p)
637 *p = strdup(type);
638 if (!p || !*p)
639 wl_client_post_no_memory(client);
640}
641
642static void
643selection_activate(struct wl_client *client,
644 struct wl_selection *selection,
645 struct wl_input_device *device, uint32_t time)
646{
647 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
648 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400649 struct wlsc_compositor *compositor =
650 (struct wlsc_compositor *) device->compositor;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500651
652 selection->input_device = device;
653
654 selection->selection_offer.object.interface =
655 &wl_selection_offer_interface;
656 selection->selection_offer.object.implementation =
657 (void (**)(void)) &selection_offer_interface;
658
659 wl_display_add_object(display, &selection->selection_offer.object);
660
661 if (wd->selection) {
662 wl_client_post_event(wd->selection->client,
663 &wd->selection->resource.object,
664 WL_SELECTION_CANCELLED);
665 }
666 wd->selection = selection;
667
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400668 wlsc_selection_set_focus(compositor->shell,
669 selection, device->keyboard_focus, time);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500670}
671
672static void
673selection_destroy(struct wl_client *client, struct wl_selection *selection)
674{
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200675 wl_resource_destroy(&selection->resource, client,
676 wlsc_compositor_get_time());
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500677}
678
679static const struct wl_selection_interface selection_interface = {
680 selection_offer,
681 selection_activate,
682 selection_destroy
683};
684
685static void
686destroy_selection(struct wl_resource *resource, struct wl_client *client)
687{
688 struct wl_selection *selection =
689 container_of(resource, struct wl_selection, resource);
690 struct wlsc_input_device *wd =
691 (struct wlsc_input_device *) selection->input_device;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400692 struct wlsc_compositor *compositor =
693 (struct wlsc_compositor *) wd->input_device.compositor;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500694
695 if (wd && wd->selection == selection) {
696 wd->selection = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400697 wlsc_selection_set_focus(compositor->shell,
698 selection, NULL,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400699 wlsc_compositor_get_time());
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500700 }
701
702 wl_list_remove(&selection->selection_focus_listener.link);
703 free(selection);
704}
705
706static void
707selection_handle_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200708 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500709{
710}
711
712static void
713shell_create_selection(struct wl_client *client,
714 struct wl_shell *shell, uint32_t id)
715{
716 struct wl_selection *selection;
717
718 selection = malloc(sizeof *selection);
719 if (selection == NULL) {
720 wl_client_post_no_memory(client);
721 return;
722 }
723
724 memset(selection, 0, sizeof *selection);
725 selection->resource.object.id = id;
726 selection->resource.object.interface = &wl_selection_interface;
727 selection->resource.object.implementation =
728 (void (**)(void)) &selection_interface;
729
730 selection->client = client;
731 selection->resource.destroy = destroy_selection;
732 selection->selection_focus = NULL;
733
734 selection->selection_focus_listener.func =
735 selection_handle_surface_destroy;
736 wl_list_init(&selection->selection_focus_listener.link);
737
738 wl_client_add_resource(client, &selection->resource);
739}
740
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500741const static struct wl_shell_interface shell_interface = {
742 shell_move,
743 shell_resize,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500744 shell_create_drag,
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400745 shell_create_selection,
746 shell_set_toplevel,
747 shell_set_transient,
748 shell_set_fullscreen
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500749};
750
Kristian Høgsberg07937562011-04-12 17:25:42 -0400751static void
752move_binding(struct wl_input_device *device, uint32_t time,
753 uint32_t key, uint32_t button, uint32_t state, void *data)
754{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400755 struct wl_shell *shell = data;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400756 struct wlsc_surface *surface =
757 (struct wlsc_surface *) device->pointer_focus;
758
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400759 if (surface == NULL)
760 return;
761
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400762 shell_move(NULL, shell, &surface->surface, device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400763}
764
765static void
766resize_binding(struct wl_input_device *device, uint32_t time,
767 uint32_t key, uint32_t button, uint32_t state, void *data)
768{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400769 struct wl_shell *shell = data;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400770 struct wlsc_surface *surface =
771 (struct wlsc_surface *) device->pointer_focus;
772 uint32_t edges = 0;
773 int32_t x, y;
774
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400775 if (surface == NULL)
776 return;
777
Kristian Høgsberg07937562011-04-12 17:25:42 -0400778 x = device->grab_x - surface->x;
779 y = device->grab_y - surface->y;
780
781 if (x < surface->width / 3)
782 edges |= WL_SHELL_RESIZE_LEFT;
783 else if (x < 2 * surface->width / 3)
784 edges |= 0;
785 else
786 edges |= WL_SHELL_RESIZE_RIGHT;
787
788 if (y < surface->height / 3)
789 edges |= WL_SHELL_RESIZE_TOP;
790 else if (y < 2 * surface->height / 3)
791 edges |= 0;
792 else
793 edges |= WL_SHELL_RESIZE_BOTTOM;
794
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400795 shell_resize(NULL, shell, &surface->surface, device, time, edges);
796}
797
798static void
799lock(struct wlsc_shell *shell)
800{
801}
802
803static void
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400804attach(struct wlsc_shell *shell, struct wlsc_surface *es)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400805{
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400806 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN) {
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400807 es->x = (es->fullscreen_output->current->width - es->width) / 2;
808 es->y = (es->fullscreen_output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400809 }
Kristian Høgsberg07937562011-04-12 17:25:42 -0400810}
811
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400812int
813shell_init(struct wlsc_compositor *ec);
814
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400815WL_EXPORT int
816shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500817{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400818 struct wl_shell *shell;
819
820 shell = malloc(sizeof *shell);
821 if (shell == NULL)
822 return -1;
823
824 shell->shell.lock = lock;
825 shell->shell.attach = attach;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400826 shell->shell.set_selection_focus = wlsc_selection_set_focus;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500827
828 shell->object.interface = &wl_shell_interface;
829 shell->object.implementation = (void (**)(void)) &shell_interface;
830 wl_display_add_object(ec->wl_display, &shell->object);
831 if (wl_display_add_global(ec->wl_display, &shell->object, NULL))
832 return -1;
833
Kristian Høgsberg07937562011-04-12 17:25:42 -0400834 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400835 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400836 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400837 resize_binding, shell);
838
839 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400840
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500841 return 0;
842}