blob: 4c959e66bcc801bb4907b04f0d7c5f7b2fb16921 [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 {
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040032 struct wlsc_shell shell;
33};
34
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050035struct wlsc_move_grab {
36 struct wl_grab grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050037 struct wlsc_surface *surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050038 int32_t dx, dy;
39};
40
41static void
42move_grab_motion(struct wl_grab *grab,
43 uint32_t time, int32_t x, int32_t y)
44{
45 struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050046 struct wlsc_surface *es = move->surface;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050047
Kristian Høgsberga691aee2011-06-23 21:43:50 -040048 wlsc_surface_configure(es, x + move->dx, y + move->dy,
49 es->width, es->height);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050050}
51
52static void
53move_grab_button(struct wl_grab *grab,
54 uint32_t time, int32_t button, int32_t state)
55{
56}
57
58static void
59move_grab_end(struct wl_grab *grab, uint32_t time)
60{
61 free(grab);
62}
63
64static const struct wl_grab_interface move_grab_interface = {
65 move_grab_motion,
66 move_grab_button,
67 move_grab_end
68};
69
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040070static int
71wlsc_surface_move(struct wlsc_surface *es,
72 struct wlsc_input_device *wd, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050073{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050074 struct wlsc_move_grab *move;
75
Kristian Høgsberg0ce24572011-01-28 15:18:33 -050076 /* FIXME: Reject if fullscreen */
77
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050078 move = malloc(sizeof *move);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040079 if (!move)
80 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050081
82 move->grab.interface = &move_grab_interface;
83 move->dx = es->x - wd->input_device.grab_x;
84 move->dy = es->y - wd->input_device.grab_y;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -050085 move->surface = es;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050086
87 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040088 &move->grab, &es->surface, time) < 0)
89 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -050090
91 wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -040092 wl_input_device_set_pointer_focus(&wd->input_device,
93 NULL, time, 0, 0, 0, 0);
94
95 return 0;
96}
97
98static void
99shell_move(struct wl_client *client, struct wl_resource *resource,
100 struct wl_resource *surface_resource,
101 struct wl_resource *input_resource, uint32_t time)
102{
103 struct wlsc_input_device *wd = input_resource->data;
104 struct wlsc_surface *es = surface_resource->data;
105
106 if (wlsc_surface_move(es, wd, time) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400107 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500108}
109
110struct wlsc_resize_grab {
111 struct wl_grab grab;
112 uint32_t edges;
113 int32_t dx, dy, width, height;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500114 struct wlsc_surface *surface;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -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øgsberg97d44aa2011-08-26 17:21:20 -0400143 wl_resource_post_event(resize->resource,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400144 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{
157 free(grab);
158}
159
160static const struct wl_grab_interface resize_grab_interface = {
161 resize_grab_motion,
162 resize_grab_button,
163 resize_grab_end
164};
165
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400166static int
167wlsc_surface_resize(struct wlsc_surface *es,
168 struct wlsc_input_device *wd,
169 uint32_t time, uint32_t edges,
170 struct wl_resource *resource)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500171{
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500172 struct wlsc_resize_grab *resize;
173 enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500174
Kristian Høgsberg0ce24572011-01-28 15:18:33 -0500175 /* FIXME: Reject if fullscreen */
176
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500177 resize = malloc(sizeof *resize);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400178 if (!resize)
179 return -1;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500180
181 resize->grab.interface = &resize_grab_interface;
182 resize->edges = edges;
183 resize->dx = es->x - wd->input_device.grab_x;
184 resize->dy = es->y - wd->input_device.grab_y;
185 resize->width = es->width;
186 resize->height = es->height;
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500187 resize->surface = es;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400188 resize->resource = resource;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400189
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500190 if (edges == 0 || edges > 15 ||
191 (edges & 3) == 3 || (edges & 12) == 12)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400192 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500193
194 switch (edges) {
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500195 case WL_SHELL_RESIZE_TOP:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500196 pointer = WLSC_POINTER_TOP;
197 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500198 case WL_SHELL_RESIZE_BOTTOM:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500199 pointer = WLSC_POINTER_BOTTOM;
200 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500201 case WL_SHELL_RESIZE_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500202 pointer = WLSC_POINTER_LEFT;
203 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500204 case WL_SHELL_RESIZE_TOP_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500205 pointer = WLSC_POINTER_TOP_LEFT;
206 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500207 case WL_SHELL_RESIZE_BOTTOM_LEFT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500208 pointer = WLSC_POINTER_BOTTOM_LEFT;
209 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500210 case WL_SHELL_RESIZE_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500211 pointer = WLSC_POINTER_RIGHT;
212 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500213 case WL_SHELL_RESIZE_TOP_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500214 pointer = WLSC_POINTER_TOP_RIGHT;
215 break;
Kristian Høgsberg027931b2011-01-21 21:57:55 -0500216 case WL_SHELL_RESIZE_BOTTOM_RIGHT:
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500217 pointer = WLSC_POINTER_BOTTOM_RIGHT;
218 break;
219 }
220
221 if (wl_input_device_update_grab(&wd->input_device,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400222 &resize->grab, &es->surface, time) < 0)
223 return 0;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500224
225 wlsc_input_device_set_pointer_image(wd, pointer);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400226 wl_input_device_set_pointer_focus(&wd->input_device,
227 NULL, time, 0, 0, 0, 0);
228
229 return 0;
230}
231
232static void
233shell_resize(struct wl_client *client, struct wl_resource *resource,
234 struct wl_resource *surface_resource,
235 struct wl_resource *input_resource, uint32_t time, uint32_t edges)
236{
237 struct wlsc_input_device *wd = input_resource->data;
238 struct wlsc_surface *es = surface_resource->data;
239
240 /* FIXME: Reject if fullscreen */
241
242 if (wlsc_surface_resize(es, wd, time, edges, resource) < 0)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400243 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500244}
245
246static void
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400247shell_set_toplevel(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400248 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400249 struct wl_resource *surface_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400250
251{
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400252 struct wlsc_surface *es = surface_resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400253 struct wlsc_compositor *ec = es->compositor;
254
255 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN) {
256 es->x = es->saved_x;
257 es->y = es->saved_y;
258 } else if (es->map_type == WLSC_SURFACE_MAP_UNMAPPED) {
259 es->x = 10 + random() % 400;
260 es->y = 10 + random() % 400;
261 /* assign to first output */
262 es->output = container_of(ec->output_list.next,
263 struct wlsc_output, link);
264 }
265
266 wlsc_surface_damage(es);
267 es->map_type = WLSC_SURFACE_MAP_TOPLEVEL;
268 es->fullscreen_output = NULL;
269}
270
271static void
272shell_set_transient(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400273 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400274 struct wl_resource *surface_resource,
275 struct wl_resource *parent_resource,
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400276 int x, int y, uint32_t flags)
277{
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400278 struct wlsc_surface *es = surface_resource->data;
279 struct wlsc_surface *pes = parent_resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400280
281 /* assign to parents output */
282 es->output = pes->output;
283
284 es->x = pes->x + x;
285 es->y = pes->y + y;
286
287 wlsc_surface_damage(es);
288 es->map_type = WLSC_SURFACE_MAP_TRANSIENT;
289}
290
291static void
292shell_set_fullscreen(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400293 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400294 struct wl_resource *surface_resource)
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400295
296{
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400297 struct wlsc_surface *es = surface_resource->data;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400298 struct wlsc_output *output;
299
300 /* FIXME: Fullscreen on first output */
301 /* FIXME: Handle output going away */
302 output = container_of(es->compositor->output_list.next,
303 struct wlsc_output, link);
304 es->output = output;
305
306 es->saved_x = es->x;
307 es->saved_y = es->y;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400308 es->x = (output->current->width - es->width) / 2;
309 es->y = (output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400310 es->fullscreen_output = output;
311 wlsc_surface_damage(es);
312 es->map_type = WLSC_SURFACE_MAP_FULLSCREEN;
313}
314
315static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400316destroy_drag(struct wl_resource *resource)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500317{
318 struct wl_drag *drag =
319 container_of(resource, struct wl_drag, resource);
320
321 wl_list_remove(&drag->drag_focus_listener.link);
322 if (drag->grab.input_device)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400323 wl_input_device_end_grab(drag->grab.input_device,
324 wlsc_compositor_get_time());
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500325
326 free(drag);
327}
328
329
330static void
331wl_drag_set_pointer_focus(struct wl_drag *drag,
332 struct wl_surface *surface, uint32_t time,
333 int32_t x, int32_t y, int32_t sx, int32_t sy)
334{
335 char **p, **end;
336
337 if (drag->drag_focus == surface)
338 return;
339
340 if (drag->drag_focus &&
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400341 (!surface ||
342 drag->drag_focus->resource.client != surface->resource.client))
343 wl_resource_post_event(&drag->drag_offer.resource,
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500344 WL_DRAG_OFFER_POINTER_FOCUS,
345 time, NULL, 0, 0, 0, 0);
346
347 if (surface &&
348 (!drag->drag_focus ||
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400349 drag->drag_focus->resource.client != surface->resource.client)) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400350
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)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400439 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500440}
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);
468
469 if (drag->target)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400470 wl_resource_post_event(&drag->drag_offer.resource,
471 WL_DRAG_OFFER_DROP);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500472
473 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
474}
475
476static const struct wl_grab_interface drag_grab_interface = {
477 drag_grab_motion,
478 drag_grab_button,
479 drag_grab_end
480};
481
482static void
483drag_activate(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400484 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400485 struct wl_resource *surface_resource,
486 struct wl_resource *device_resource, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500487{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400488 struct wl_drag *drag = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400489 struct wl_surface *surface = surface_resource->data;
490 struct wl_input_device *device = device_resource->data;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500491 struct wl_display *display = wl_client_get_display (client);
492 struct wlsc_surface *target;
493 int32_t sx, sy;
494
495 if (wl_input_device_update_grab(device,
496 &drag->grab, surface, time) < 0)
497 return;
498
499 drag->grab.interface = &drag_grab_interface;
500
501 drag->source = surface;
502
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400503 drag->drag_offer.resource.object.interface = &wl_drag_offer_interface;
504 drag->drag_offer.resource.object.implementation =
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500505 (void (**)(void)) &drag_offer_interface;
506
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400507 wl_display_add_global(display, &wl_drag_offer_interface, drag, NULL);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500508
509 target = pick_surface(device, &sx, &sy);
Kristian Høgsbergdd4046a2011-01-21 17:00:09 -0500510 wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500511 wl_drag_set_pointer_focus(drag, &target->surface, time,
512 device->x, device->y, sx, sy);
513}
514
515static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400516drag_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500517{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400518 wl_resource_destroy(resource, wlsc_compositor_get_time());
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500519}
520
521static const struct wl_drag_interface drag_interface = {
522 drag_offer,
523 drag_activate,
524 drag_destroy,
525};
526
527static void
528drag_handle_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200529 struct wl_resource *resource, uint32_t time)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500530{
531 struct wl_drag *drag =
532 container_of(listener, struct wl_drag, drag_focus_listener);
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200533 struct wl_surface *surface = (struct wl_surface *) resource;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500534
535 if (drag->drag_focus == surface)
536 wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0);
537}
538
539static void
540shell_create_drag(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400541 struct wl_resource *resource, uint32_t id)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500542{
543 struct wl_drag *drag;
544
545 drag = malloc(sizeof *drag);
546 if (drag == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400547 wl_resource_post_no_memory(resource);
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500548 return;
549 }
550
551 memset(drag, 0, sizeof *drag);
552 drag->resource.object.id = id;
553 drag->resource.object.interface = &wl_drag_interface;
554 drag->resource.object.implementation =
555 (void (**)(void)) &drag_interface;
556
557 drag->resource.destroy = destroy_drag;
558
559 drag->drag_focus_listener.func = drag_handle_surface_destroy;
560 wl_list_init(&drag->drag_focus_listener.link);
561
562 wl_client_add_resource(client, &drag->resource);
563}
564
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400565static void
566wlsc_selection_set_focus(struct wlsc_shell *shell,
567 struct wl_selection *selection,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500568 struct wl_surface *surface, uint32_t time)
569{
570 char **p, **end;
571
572 if (selection->selection_focus == surface)
573 return;
574
575 if (selection->selection_focus != NULL)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400576 wl_resource_post_event(&selection->selection_offer.resource,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500577 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
578 NULL);
579
580 if (surface) {
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500581
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400582 selection->selection_offer.resource.client = surface->resource.client;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500583 end = selection->types.data + selection->types.size;
584 for (p = selection->types.data; p < end; p++)
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400585 wl_resource_post_event(&selection->selection_offer.resource,
586 WL_SELECTION_OFFER_OFFER, *p);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500587
588 wl_list_remove(&selection->selection_focus_listener.link);
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200589 wl_list_insert(surface->resource.destroy_listener_list.prev,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500590 &selection->selection_focus_listener.link);
591
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400592 wl_resource_post_event(&selection->selection_offer.resource,
593 WL_SELECTION_OFFER_KEYBOARD_FOCUS,
594 selection->input_device);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500595 }
596
597 selection->selection_focus = surface;
598
599 wl_list_remove(&selection->selection_focus_listener.link);
600 if (surface)
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200601 wl_list_insert(surface->resource.destroy_listener_list.prev,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500602 &selection->selection_focus_listener.link);
603}
604
605static void
606selection_offer_receive(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400607 struct wl_resource *resource,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500608 const char *mime_type, int fd)
609{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400610 struct wl_selection_offer *offer = resource->data;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500611 struct wl_selection *selection =
612 container_of(offer, struct wl_selection, selection_offer);
613
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400614 wl_resource_post_event(&selection->resource,
615 WL_SELECTION_SEND, mime_type, fd);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500616 close(fd);
617}
618
619static const struct wl_selection_offer_interface selection_offer_interface = {
620 selection_offer_receive
621};
622
623static void
624selection_offer(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400625 struct wl_resource *resource, const char *type)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500626{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400627 struct wl_selection *selection = resource->data;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500628 char **p;
629
630 p = wl_array_add(&selection->types, sizeof *p);
631 if (p)
632 *p = strdup(type);
633 if (!p || !*p)
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400634 wl_resource_post_no_memory(resource);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500635}
636
637static void
638selection_activate(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400639 struct wl_resource *resource,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400640 struct wl_resource *input_resource, uint32_t time)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500641{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400642 struct wl_selection *selection = resource->data;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400643 struct wlsc_input_device *wd = input_resource->data;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500644 struct wl_display *display = wl_client_get_display (client);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400645 struct wlsc_compositor *compositor =
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400646 (struct wlsc_compositor *) wd->input_device.compositor;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500647
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400648 selection->input_device = &wd->input_device;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500649
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400650 selection->selection_offer.resource.object.interface =
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500651 &wl_selection_offer_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400652 selection->selection_offer.resource.object.implementation =
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500653 (void (**)(void)) &selection_offer_interface;
654
Kristian Høgsbergd9551a32011-08-19 12:07:44 -0400655 wl_display_add_global(display,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400656 &wl_selection_offer_interface, selection, NULL);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500657
658 if (wd->selection) {
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400659 wl_resource_post_event(&wd->selection->resource,
660 WL_SELECTION_CANCELLED);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500661 }
662 wd->selection = selection;
663
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400664 wlsc_selection_set_focus(compositor->shell, selection,
665 wd->input_device.keyboard_focus, time);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500666}
667
668static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400669selection_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500670{
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400671 wl_resource_destroy(resource, wlsc_compositor_get_time());
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500672}
673
674static const struct wl_selection_interface selection_interface = {
675 selection_offer,
676 selection_activate,
677 selection_destroy
678};
679
680static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400681destroy_selection(struct wl_resource *resource)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500682{
683 struct wl_selection *selection =
684 container_of(resource, struct wl_selection, resource);
685 struct wlsc_input_device *wd =
686 (struct wlsc_input_device *) selection->input_device;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400687 struct wlsc_compositor *compositor =
688 (struct wlsc_compositor *) wd->input_device.compositor;
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500689
690 if (wd && wd->selection == selection) {
691 wd->selection = NULL;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400692 wlsc_selection_set_focus(compositor->shell,
693 selection, NULL,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400694 wlsc_compositor_get_time());
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500695 }
696
697 wl_list_remove(&selection->selection_focus_listener.link);
698 free(selection);
699}
700
701static void
702selection_handle_surface_destroy(struct wl_listener *listener,
Benjamin Franzke4721a3c2011-05-06 17:13:17 +0200703 struct wl_resource *resource, uint32_t time)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500704{
705}
706
707static void
708shell_create_selection(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400709 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500710{
711 struct wl_selection *selection;
712
713 selection = malloc(sizeof *selection);
714 if (selection == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -0400715 wl_resource_post_no_memory(resource);
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500716 return;
717 }
718
719 memset(selection, 0, sizeof *selection);
720 selection->resource.object.id = id;
721 selection->resource.object.interface = &wl_selection_interface;
722 selection->resource.object.implementation =
723 (void (**)(void)) &selection_interface;
724
725 selection->client = client;
726 selection->resource.destroy = destroy_selection;
727 selection->selection_focus = NULL;
728
729 selection->selection_focus_listener.func =
730 selection_handle_surface_destroy;
731 wl_list_init(&selection->selection_focus_listener.link);
732
733 wl_client_add_resource(client, &selection->resource);
734}
735
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500736const static struct wl_shell_interface shell_interface = {
737 shell_move,
738 shell_resize,
Kristian Høgsbergae6c8a62011-01-18 09:08:53 -0500739 shell_create_drag,
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400740 shell_create_selection,
741 shell_set_toplevel,
742 shell_set_transient,
743 shell_set_fullscreen
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500744};
745
Kristian Høgsberg07937562011-04-12 17:25:42 -0400746static void
747move_binding(struct wl_input_device *device, uint32_t time,
748 uint32_t key, uint32_t button, uint32_t state, void *data)
749{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400750 struct wlsc_surface *surface =
751 (struct wlsc_surface *) device->pointer_focus;
752
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400753 if (surface == NULL)
754 return;
755
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400756 wlsc_surface_move(surface, (struct wlsc_input_device *) device, time);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400757}
758
759static void
760resize_binding(struct wl_input_device *device, uint32_t time,
761 uint32_t key, uint32_t button, uint32_t state, void *data)
762{
Kristian Høgsberg07937562011-04-12 17:25:42 -0400763 struct wlsc_surface *surface =
764 (struct wlsc_surface *) device->pointer_focus;
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400765 struct wl_resource *resource;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400766 uint32_t edges = 0;
767 int32_t x, y;
768
Kristian Høgsberg10f097e2011-04-13 11:52:54 -0400769 if (surface == NULL)
770 return;
771
Kristian Høgsberg07937562011-04-12 17:25:42 -0400772 x = device->grab_x - surface->x;
773 y = device->grab_y - surface->y;
774
775 if (x < surface->width / 3)
776 edges |= WL_SHELL_RESIZE_LEFT;
777 else if (x < 2 * surface->width / 3)
778 edges |= 0;
779 else
780 edges |= WL_SHELL_RESIZE_RIGHT;
781
782 if (y < surface->height / 3)
783 edges |= WL_SHELL_RESIZE_TOP;
784 else if (y < 2 * surface->height / 3)
785 edges |= 0;
786 else
787 edges |= WL_SHELL_RESIZE_BOTTOM;
788
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400789 resource = /* Find shell resource for surface client */ 0;
790
791 /* ... or use wl_shell_surface */
792
793 wlsc_surface_resize(surface, (struct wlsc_input_device *) device,
794 time, edges, resource);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400795}
796
797static void
798lock(struct wlsc_shell *shell)
799{
800}
801
802static void
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400803attach(struct wlsc_shell *shell, struct wlsc_surface *es)
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400804{
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400805 if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN) {
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400806 es->x = (es->fullscreen_output->current->width - es->width) / 2;
807 es->y = (es->fullscreen_output->current->height - es->height) / 2;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400808 }
Kristian Høgsberg07937562011-04-12 17:25:42 -0400809}
810
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400811static void
812bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id)
813{
814 struct wl_shell *shell = data;
815
816 wl_client_add_object(client, &wl_shell_interface,
817 &shell_interface, id, shell);
818}
819
Kristian Høgsberg6c709a32011-05-06 14:52:41 -0400820int
821shell_init(struct wlsc_compositor *ec);
822
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400823WL_EXPORT int
824shell_init(struct wlsc_compositor *ec)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500825{
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400826 struct wl_shell *shell;
827
828 shell = malloc(sizeof *shell);
829 if (shell == NULL)
830 return -1;
831
832 shell->shell.lock = lock;
833 shell->shell.attach = attach;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400834 shell->shell.set_selection_focus = wlsc_selection_set_focus;
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500835
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400836 if (wl_display_add_global(ec->wl_display, &wl_shell_interface,
837 shell, bind_shell) == NULL)
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500838 return -1;
839
Kristian Høgsberg07937562011-04-12 17:25:42 -0400840 wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400841 move_binding, shell);
Kristian Høgsberg07937562011-04-12 17:25:42 -0400842 wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER,
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400843 resize_binding, shell);
844
845 ec->shell = &shell->shell;
Kristian Høgsberg07937562011-04-12 17:25:42 -0400846
Kristian Høgsberg4cca3492011-01-18 07:53:49 -0500847 return 0;
848}