blob: 251d17a7dda691ac552a6ab68347df2223d3f5d6 [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øgsberg904055a2011-08-18 17:55:30 -040080shell_move(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050081 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øgsberg904055a2011-08-18 17:55:30 -0400115 struct wl_resource resource;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500116};
117
118static void
119resize_grab_motion(struct wl_grab *grab,
120 uint32_t time, int32_t x, int32_t y)
121{
122 struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab;
123 struct wl_input_device *device = grab->input_device;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500124 struct wl_surface *surface = &resize->surface->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500125 int32_t width, height;
126
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500127 if (resize->edges & WL_SHELL_RESIZE_LEFT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500128 width = device->grab_x - x + resize->width;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500129 } else if (resize->edges & WL_SHELL_RESIZE_RIGHT) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500130 width = x - device->grab_x + resize->width;
131 } else {
132 width = resize->width;
133 }
134
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500135 if (resize->edges & WL_SHELL_RESIZE_TOP) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500136 height = device->grab_y - y + resize->height;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500137 } else if (resize->edges & WL_SHELL_RESIZE_BOTTOM) {
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500138 height = y - device->grab_y + resize->height;
139 } else {
140 height = resize->height;
141 }
142
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400143 wl_resource_post_event(&resize->resource,
144 WL_SHELL_CONFIGURE, time, resize->edges,
145 surface, width, height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500146}
147
148static void
149resize_grab_button(struct wl_grab *grab,
150 uint32_t time, int32_t button, int32_t state)
151{
152}
153
154static void
155resize_grab_end(struct wl_grab *grab, uint32_t time)
156{
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500157 struct wlsc_surface *es;
158 struct wl_input_device *device = grab->input_device;
159 int32_t sx, sy;
160
161 es = pick_surface(grab->input_device, &sx, &sy);
162 wl_input_device_set_pointer_focus(device,
163 &es->surface, time,
164 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500165 free(grab);
166}
167
168static const struct wl_grab_interface resize_grab_interface = {
169 resize_grab_motion,
170 resize_grab_button,
171 resize_grab_end
172};
173
Kristian Høgsberg07937562011-04-12 17:25:42 -0400174static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400175shell_resize(struct wl_client *client, struct wl_resource *resource,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500176 struct wl_surface *surface,
177 struct wl_input_device *device, uint32_t time, uint32_t edges)
178{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400179 struct wl_shell *shell = resource->data;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500180 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;
184
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500185 /* FIXME: Reject if fullscreen */
186
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500187 resize = malloc(sizeof *resize);
188 if (!resize) {
189 wl_client_post_no_memory(client);
190 return;
191 }
192
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;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500199 resize->surface = es;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400200 resize->shell = shell;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500201
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400202 resize->resource.object = resource->object;
203 resize->resource.client = client;
204
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500205 if (edges == 0 || edges > 15 ||
206 (edges & 3) == 3 || (edges & 12) == 12)
207 return;
208
209 switch (edges) {
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500210 case WL_SHELL_RESIZE_TOP:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500211 pointer = WLSC_POINTER_TOP;
212 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500213 case WL_SHELL_RESIZE_BOTTOM:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500214 pointer = WLSC_POINTER_BOTTOM;
215 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500216 case WL_SHELL_RESIZE_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500217 pointer = WLSC_POINTER_LEFT;
218 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500219 case WL_SHELL_RESIZE_TOP_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500220 pointer = WLSC_POINTER_TOP_LEFT;
221 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500222 case WL_SHELL_RESIZE_BOTTOM_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500223 pointer = WLSC_POINTER_BOTTOM_LEFT;
224 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500225 case WL_SHELL_RESIZE_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500226 pointer = WLSC_POINTER_RIGHT;
227 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500228 case WL_SHELL_RESIZE_TOP_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500229 pointer = WLSC_POINTER_TOP_RIGHT;
230 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500231 case WL_SHELL_RESIZE_BOTTOM_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500232 pointer = WLSC_POINTER_BOTTOM_RIGHT;
233 break;
234 }
235
236 if (wl_input_device_update_grab(&wd->input_device,
237 &resize->grab, surface, time) < 0)
238 return;
239
240 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500241 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500242}
243
244static void
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400245shell_set_toplevel(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400246 struct wl_resource *resource,
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400247 struct wl_surface *surface)
248
249{
250 struct wlsc_surface *es = (struct wlsc_surface *) surface;
251 struct wlsc_compositor *ec = es->compositor;
252
253 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN) {
254 es->x = es->saved_x;
255 es->y = es->saved_y;
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);
262 }
263
264 wlsc_surface_damage(es);
265 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
266 es->fullscreen_output = NULL;
267}
268
269static void
270shell_set_transient(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400271 struct wl_resource *resource,
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400272 struct wl_surface *surface,
273 struct wl_surface *parent,
274 int x, int y, uint32_t flags)
275{
276 struct wlsc_surface *es = (struct wlsc_surface *) surface;
277 struct wlsc_surface *pes = (struct wlsc_surface *) parent;
278
279 /* assign to parents output */
280 es->output = pes->output;
281
282 es->x = pes->x + x;
283 es->y = pes->y + y;
284
285 wlsc_surface_damage(es);
286 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
287}
288
289static void
290shell_set_fullscreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400291 struct wl_resource *resource,
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400292 struct wl_surface *surface)
293
294{
295 struct wlsc_surface *es = (struct wlsc_surface *) surface;
296 struct wlsc_output *output;
297
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);
302 es->output = output;
303
304 es->saved_x = es->x;
305 es->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400306 es->x = (output->current->width - es->width) / 2;
307 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400308 es->fullscreen_output = output;
309 wlsc_surface_damage(es);
310 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
311}
312
313static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400314destroy_drag(struct wl_resource *resource)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500315{
316 struct wl_drag *drag =
317 container_of(resource, struct wl_drag, resource);
318
319 wl_list_remove(&drag->drag_focus_listener.link);
320 if (drag->grab.input_device)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400321 wl_input_device_end_grab(drag->grab.input_device,
322 wlsc_compositor_get_time());
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500323
324 free(drag);
325}
326
327
328static void
329wl_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)
332{
333 char **p, **end;
334
335 if (drag->drag_focus == surface)
336 return;
337
338 if (drag->drag_focus &&
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400339 (!surface ||
340 drag->drag_focus->resource.client != surface->resource.client))
341 wl_resource_post_event(&drag->drag_offer.resource,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500342 WL_DRAG_OFFER_POINTER_FOCUS,
343 time, NULL, 0, 0, 0, 0);
344
345 if (surface &&
346 (!drag->drag_focus ||
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400347 drag->drag_focus->resource.client != surface->resource.client)) {
348 wl_client_post_global(surface->resource.client,
349 &drag->drag_offer.resource.object);
350
351 drag->drag_offer.resource.client = surface->resource.client;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500352 end = drag->types.data + drag->types.size;
353 for (p = drag->types.data; p < end; p++)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400354 wl_resource_post_event(&drag->drag_offer.resource,
355 WL_DRAG_OFFER_OFFER, *p);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500356 }
357
358 if (surface) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400359 wl_resource_post_event(&drag->drag_offer.resource,
360 WL_DRAG_OFFER_POINTER_FOCUS,
361 time, surface,
362 x, y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500363
364 }
365
366 drag->drag_focus = surface;
367 drag->pointer_focus_time = time;
368 drag->target = NULL;
369
370 wl_list_remove(&drag->drag_focus_listener.link);
371 if (surface)
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200372 wl_list_insert(surface->resource.destroy_listener_list.prev,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500373 &drag->drag_focus_listener.link);
374}
375
376static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400377drag_offer_accept(struct wl_client *client, struct wl_resource *resource,
378 uint32_t time, const char *type)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500379{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400380 struct wl_drag_offer *offer = resource->data;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500381 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
382 char **p, **end;
383
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)
390 return;
391
392 drag->target = client;
393 drag->type = NULL;
394 end = drag->types.data + drag->types.size;
395 for (p = drag->types.data; p < end; p++)
396 if (type && strcmp(*p, type) == 0)
397 drag->type = *p;
398
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400399 wl_resource_post_event(&drag->resource, WL_DRAG_TARGET, drag->type);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500400}
401
402static void
403drag_offer_receive(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400404 struct wl_resource *resource, int fd)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500405{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400406 struct wl_drag_offer *offer = resource->data;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500407 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
408
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400409 wl_resource_post_event(&drag->resource, WL_DRAG_FINISH, fd);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500410 close(fd);
411}
412
413static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400414drag_offer_reject(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500415{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400416 struct wl_drag_offer *offer = resource->data;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500417 struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer);
418
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400419 wl_resource_post_event(&drag->resource, WL_DRAG_REJECT);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500420}
421
422static const struct wl_drag_offer_interface drag_offer_interface = {
423 drag_offer_accept,
424 drag_offer_receive,
425 drag_offer_reject
426};
427
428static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400429drag_offer(struct wl_client *client,
430 struct wl_resource *resource, const char *type)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500431{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400432 struct wl_drag *drag = resource->data;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500433 char **p;
434
435 p = wl_array_add(&drag->types, sizeof *p);
436 if (p)
437 *p = strdup(type);
438 if (!p || !*p)
439 wl_client_post_no_memory(client);
440}
441
442static void
443drag_grab_motion(struct wl_grab *grab,
444 uint32_t time, int32_t x, int32_t y)
445{
446 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
447 struct wlsc_surface *es;
448 int32_t sx, sy;
449
450 es = pick_surface(grab->input_device, &sx, &sy);
451 wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy);
452 if (es)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400453 wl_resource_post_event(&drag->drag_offer.resource,
454 WL_DRAG_OFFER_MOTION,
455 time, x, y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500456}
457
458static void
459drag_grab_button(struct wl_grab *grab,
460 uint32_t time, int32_t button, int32_t state)
461{
462}
463
464static void
465drag_grab_end(struct wl_grab *grab, uint32_t time)
466{
467 struct wl_drag *drag = container_of(grab, struct wl_drag, grab);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500468 struct wlsc_surface *es;
469 struct wl_input_device *device = grab->input_device;
470 int32_t sx, sy;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500471
472 if (drag->target)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400473 wl_resource_post_event(&drag->drag_offer.resource,
474 WL_DRAG_OFFER_DROP);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500475
476 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500477
478 es = pick_surface(grab->input_device, &sx, &sy);
479 wl_input_device_set_pointer_focus(device,
480 &es->surface, time,
481 device->x, device->y, sx, sy);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500482}
483
484static const struct wl_grab_interface drag_grab_interface = {
485 drag_grab_motion,
486 drag_grab_button,
487 drag_grab_end
488};
489
490static void
491drag_activate(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400492 struct wl_resource *resource,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500493 struct wl_surface *surface,
494 struct wl_input_device *device, uint32_t time)
495{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400496 struct wl_drag *drag = resource->data;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500497 struct wl_display *display = wl_client_get_display (client);
498 struct wlsc_surface *target;
499 int32_t sx, sy;
500
501 if (wl_input_device_update_grab(device,
502 &drag->grab, surface, time) < 0)
503 return;
504
505 drag->grab.interface = &drag_grab_interface;
506
507 drag->source = surface;
508
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400509 drag->drag_offer.resource.object.interface = &wl_drag_offer_interface;
510 drag->drag_offer.resource.object.implementation =
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500511 (void (**)(void)) &drag_offer_interface;
512
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400513 wl_display_add_object(display, &drag->drag_offer.resource.object);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500514
515 target = pick_surface(device, &sx, &sy);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500516 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500517 wl_drag_set_pointer_focus(drag, &target->surface, time,
518 device->x, device->y, sx, sy);
519}
520
521static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400522drag_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500523{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400524 wl_resource_destroy(resource, wlsc_compositor_get_time());
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500525}
526
527static const struct wl_drag_interface drag_interface = {
528 drag_offer,
529 drag_activate,
530 drag_destroy,
531};
532
533static void
534drag_handle_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200535 struct wl_resource *resource, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500536{
537 struct wl_drag *drag =
538 container_of(listener, struct wl_drag, drag_focus_listener);
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200539 struct wl_surface *surface = (struct wl_surface *) resource;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500540
541 if (drag->drag_focus == surface)
542 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
543}
544
545static void
546shell_create_drag(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400547 struct wl_resource *resource, uint32_t id)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500548{
549 struct wl_drag *drag;
550
551 drag = malloc(sizeof *drag);
552 if (drag == NULL) {
553 wl_client_post_no_memory(client);
554 return;
555 }
556
557 memset(drag, 0, sizeof *drag);
558 drag->resource.object.id = id;
559 drag->resource.object.interface = &wl_drag_interface;
560 drag->resource.object.implementation =
561 (void (**)(void)) &drag_interface;
562
563 drag->resource.destroy = destroy_drag;
564
565 drag->drag_focus_listener.func = drag_handle_surface_destroy;
566 wl_list_init(&drag->drag_focus_listener.link);
567
568 wl_client_add_resource(client, &drag->resource);
569}
570
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400571static void
572wlsc_selection_set_focus(struct wlsc_shell *shell,
573 struct wl_selection *selection,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500574 struct wl_surface *surface, uint32_t time)
575{
576 char **p, **end;
577
578 if (selection->selection_focus == surface)
579 return;
580
581 if (selection->selection_focus != NULL)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400582 wl_resource_post_event(&selection->selection_offer.resource,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500583 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
584 NULL);
585
586 if (surface) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400587 wl_client_post_global(surface->resource.client,
588 &selection->selection_offer.resource.object);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500589
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400590 selection->selection_offer.resource.client = surface->resource.client;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500591 end = selection->types.data + selection->types.size;
592 for (p = selection->types.data; p < end; p++)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400593 wl_resource_post_event(&selection->selection_offer.resource,
594 WL_SELECTION_OFFER_OFFER, *p);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500595
596 wl_list_remove(&selection->selection_focus_listener.link);
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200597 wl_list_insert(surface->resource.destroy_listener_list.prev,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500598 &selection->selection_focus_listener.link);
599
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400600 wl_resource_post_event(&selection->selection_offer.resource,
601 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
602 selection->input_device);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500603 }
604
605 selection->selection_focus = surface;
606
607 wl_list_remove(&selection->selection_focus_listener.link);
608 if (surface)
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200609 wl_list_insert(surface->resource.destroy_listener_list.prev,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500610 &selection->selection_focus_listener.link);
611}
612
613static void
614selection_offer_receive(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400615 struct wl_resource *resource,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500616 const char *mime_type, int fd)
617{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400618 struct wl_selection_offer *offer = resource->data;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500619 struct wl_selection *selection =
620 container_of(offer, struct wl_selection, selection_offer);
621
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400622 wl_resource_post_event(&selection->resource,
623 WL_SELECTION_SEND, mime_type, fd);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500624 close(fd);
625}
626
627static const struct wl_selection_offer_interface selection_offer_interface = {
628 selection_offer_receive
629};
630
631static void
632selection_offer(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400633 struct wl_resource *resource, const char *type)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500634{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400635 struct wl_selection *selection = resource->data;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500636 char **p;
637
638 p = wl_array_add(&selection->types, sizeof *p);
639 if (p)
640 *p = strdup(type);
641 if (!p || !*p)
642 wl_client_post_no_memory(client);
643}
644
645static void
646selection_activate(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400647 struct wl_resource *resource,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500648 struct wl_input_device *device, uint32_t time)
649{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400650 struct wl_selection *selection = resource->data;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500651 struct wlsc_input_device *wd = (struct wlsc_input_device *) device;
652 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400653 struct wlsc_compositor *compositor =
654 (struct wlsc_compositor *) device->compositor;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500655
656 selection->input_device = device;
657
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400658 selection->selection_offer.resource.object.interface =
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500659 &wl_selection_offer_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400660 selection->selection_offer.resource.object.implementation =
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500661 (void (**)(void)) &selection_offer_interface;
662
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400663 wl_display_add_object(display,
664 &selection->selection_offer.resource.object);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500665
666 if (wd->selection) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400667 wl_resource_post_event(&wd->selection->resource,
668 WL_SELECTION_CANCELLED);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500669 }
670 wd->selection = selection;
671
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400672 wlsc_selection_set_focus(compositor->shell,
673 selection, device->keyboard_focus, time);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500674}
675
676static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400677selection_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500678{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400679 wl_resource_destroy(resource, wlsc_compositor_get_time());
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500680}
681
682static const struct wl_selection_interface selection_interface = {
683 selection_offer,
684 selection_activate,
685 selection_destroy
686};
687
688static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400689destroy_selection(struct wl_resource *resource)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500690{
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;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400695 struct wlsc_compositor *compositor =
696 (struct wlsc_compositor *) wd->input_device.compositor;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500697
698 if (wd && wd->selection == selection) {
699 wd->selection = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400700 wlsc_selection_set_focus(compositor->shell,
701 selection, NULL,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400702 wlsc_compositor_get_time());
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500703 }
704
705 wl_list_remove(&selection->selection_focus_listener.link);
706 free(selection);
707}
708
709static void
710selection_handle_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200711 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500712{
713}
714
715static void
716shell_create_selection(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400717 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500718{
719 struct wl_selection *selection;
720
721 selection = malloc(sizeof *selection);
722 if (selection == NULL) {
723 wl_client_post_no_memory(client);
724 return;
725 }
726
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;
732
733 selection->client = client;
734 selection->resource.destroy = destroy_selection;
735 selection->selection_focus = NULL;
736
737 selection->selection_focus_listener.func =
738 selection_handle_surface_destroy;
739 wl_list_init(&selection->selection_focus_listener.link);
740
741 wl_client_add_resource(client, &selection->resource);
742}
743
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500744const static struct wl_shell_interface shell_interface = {
745 shell_move,
746 shell_resize,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500747 shell_create_drag,
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400748 shell_create_selection,
749 shell_set_toplevel,
750 shell_set_transient,
751 shell_set_fullscreen
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500752};
753
Kristian Høgsberg07937562011-04-12 17:25:42 -0400754static void
755move_binding(struct wl_input_device *device, uint32_t time,
756 uint32_t key, uint32_t button, uint32_t state, void *data)
757{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400758 struct wlsc_surface *surface =
759 (struct wlsc_surface *) device->pointer_focus;
760
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400761 if (surface == NULL)
762 return;
763
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400764 shell_move(NULL, NULL, &surface->surface, device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400765}
766
767static void
768resize_binding(struct wl_input_device *device, uint32_t time,
769 uint32_t key, uint32_t button, uint32_t state, void *data)
770{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400771 struct wlsc_surface *surface =
772 (struct wlsc_surface *) device->pointer_focus;
773 uint32_t edges = 0;
774 int32_t x, y;
775
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400776 if (surface == NULL)
777 return;
778
Kristian Høgsberg07937562011-04-12 17:25:42 -0400779 x = device->grab_x - surface->x;
780 y = device->grab_y - surface->y;
781
782 if (x < surface->width / 3)
783 edges |= WL_SHELL_RESIZE_LEFT;
784 else if (x < 2 * surface->width / 3)
785 edges |= 0;
786 else
787 edges |= WL_SHELL_RESIZE_RIGHT;
788
789 if (y < surface->height / 3)
790 edges |= WL_SHELL_RESIZE_TOP;
791 else if (y < 2 * surface->height / 3)
792 edges |= 0;
793 else
794 edges |= WL_SHELL_RESIZE_BOTTOM;
795
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400796 shell_resize(NULL, NULL, &surface->surface, device, time, edges);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400797}
798
799static void
800lock(struct wlsc_shell *shell)
801{
802}
803
804static void
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400805attach(struct wlsc_shell *shell, struct wlsc_surface *es)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400806{
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400807 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN) {
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400808 es->x = (es->fullscreen_output->current->width - es->width) / 2;
809 es->y = (es->fullscreen_output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400810 }
Kristian Høgsberg07937562011-04-12 17:25:42 -0400811}
812
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400813int
814shell_init(struct wlsc_compositor *ec);
815
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400816WL_EXPORT int
817shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500818{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400819 struct wl_shell *shell;
820
821 shell = malloc(sizeof *shell);
822 if (shell == NULL)
823 return -1;
824
825 shell->shell.lock = lock;
826 shell->shell.attach = attach;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400827 shell->shell.set_selection_focus = wlsc_selection_set_focus;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500828
829 shell->object.interface = &wl_shell_interface;
830 shell->object.implementation = (void (**)(void)) &shell_interface;
831 wl_display_add_object(ec->wl_display, &shell->object);
832 if (wl_display_add_global(ec->wl_display, &shell->object, NULL))
833 return -1;
834
Kristian Høgsberg07937562011-04-12 17:25:42 -0400835 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400836 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400837 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400838 resize_binding, shell);
839
840 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400841
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500842 return 0;
843}