Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include "wayland-server.h" |
| 28 | #include "compositor.h" |
| 29 | |
| 30 | struct wlsc_move_grab { |
| 31 | struct wl_grab grab; |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 32 | struct wlsc_surface *surface; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 33 | int32_t dx, dy; |
| 34 | }; |
| 35 | |
| 36 | static void |
| 37 | move_grab_motion(struct wl_grab *grab, |
| 38 | uint32_t time, int32_t x, int32_t y) |
| 39 | { |
| 40 | struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab; |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 41 | struct wlsc_surface *es = move->surface; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 42 | |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame^] | 43 | wlsc_surface_damage(es); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 44 | es->x = x + move->dx; |
| 45 | es->y = y + move->dy; |
| 46 | wlsc_surface_update_matrix(es); |
Kristian Høgsberg | 31bd6c7 | 2011-02-13 13:00:51 -0500 | [diff] [blame^] | 47 | wlsc_surface_damage(es); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static void |
| 51 | move_grab_button(struct wl_grab *grab, |
| 52 | uint32_t time, int32_t button, int32_t state) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | static void |
| 57 | move_grab_end(struct wl_grab *grab, uint32_t time) |
| 58 | { |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 59 | struct wlsc_surface *es; |
| 60 | struct wl_input_device *device = grab->input_device; |
| 61 | int32_t sx, sy; |
| 62 | |
| 63 | es = pick_surface(grab->input_device, &sx, &sy); |
| 64 | wl_input_device_set_pointer_focus(device, |
| 65 | &es->surface, time, |
| 66 | device->x, device->y, sx, sy); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 67 | free(grab); |
| 68 | } |
| 69 | |
| 70 | static const struct wl_grab_interface move_grab_interface = { |
| 71 | move_grab_motion, |
| 72 | move_grab_button, |
| 73 | move_grab_end |
| 74 | }; |
| 75 | |
| 76 | void |
| 77 | shell_move(struct wl_client *client, struct wl_shell *shell, |
| 78 | struct wl_surface *surface, |
| 79 | struct wl_input_device *device, uint32_t time) |
| 80 | { |
| 81 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
| 82 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 83 | struct wlsc_move_grab *move; |
| 84 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 85 | /* FIXME: Reject if fullscreen */ |
| 86 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 87 | move = malloc(sizeof *move); |
| 88 | if (!move) { |
| 89 | wl_client_post_no_memory(client); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | move->grab.interface = &move_grab_interface; |
| 94 | move->dx = es->x - wd->input_device.grab_x; |
| 95 | move->dy = es->y - wd->input_device.grab_y; |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 96 | move->surface = es; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 97 | |
| 98 | if (wl_input_device_update_grab(&wd->input_device, |
| 99 | &move->grab, surface, time) < 0) |
| 100 | return; |
| 101 | |
| 102 | wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING); |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 103 | wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | struct wlsc_resize_grab { |
| 107 | struct wl_grab grab; |
| 108 | uint32_t edges; |
| 109 | int32_t dx, dy, width, height; |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 110 | struct wlsc_surface *surface; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | static void |
| 114 | resize_grab_motion(struct wl_grab *grab, |
| 115 | uint32_t time, int32_t x, int32_t y) |
| 116 | { |
| 117 | struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab; |
| 118 | struct wl_input_device *device = grab->input_device; |
| 119 | struct wlsc_compositor *ec = |
| 120 | (struct wlsc_compositor *) device->compositor; |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 121 | struct wl_surface *surface = &resize->surface->surface; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 122 | int32_t width, height; |
| 123 | |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 124 | if (resize->edges & WL_SHELL_RESIZE_LEFT) { |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 125 | width = device->grab_x - x + resize->width; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 126 | } else if (resize->edges & WL_SHELL_RESIZE_RIGHT) { |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 127 | width = x - device->grab_x + resize->width; |
| 128 | } else { |
| 129 | width = resize->width; |
| 130 | } |
| 131 | |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 132 | if (resize->edges & WL_SHELL_RESIZE_TOP) { |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 133 | height = device->grab_y - y + resize->height; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 134 | } else if (resize->edges & WL_SHELL_RESIZE_BOTTOM) { |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 135 | height = y - device->grab_y + resize->height; |
| 136 | } else { |
| 137 | height = resize->height; |
| 138 | } |
| 139 | |
| 140 | wl_client_post_event(surface->client, &ec->shell.object, |
| 141 | WL_SHELL_CONFIGURE, time, resize->edges, |
| 142 | surface, width, height); |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | resize_grab_button(struct wl_grab *grab, |
| 147 | uint32_t time, int32_t button, int32_t state) |
| 148 | { |
| 149 | } |
| 150 | |
| 151 | static void |
| 152 | resize_grab_end(struct wl_grab *grab, uint32_t time) |
| 153 | { |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 154 | struct wlsc_surface *es; |
| 155 | struct wl_input_device *device = grab->input_device; |
| 156 | int32_t sx, sy; |
| 157 | |
| 158 | es = pick_surface(grab->input_device, &sx, &sy); |
| 159 | wl_input_device_set_pointer_focus(device, |
| 160 | &es->surface, time, |
| 161 | device->x, device->y, sx, sy); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 162 | free(grab); |
| 163 | } |
| 164 | |
| 165 | static const struct wl_grab_interface resize_grab_interface = { |
| 166 | resize_grab_motion, |
| 167 | resize_grab_button, |
| 168 | resize_grab_end |
| 169 | }; |
| 170 | |
| 171 | void |
| 172 | shell_resize(struct wl_client *client, struct wl_shell *shell, |
| 173 | struct wl_surface *surface, |
| 174 | struct wl_input_device *device, uint32_t time, uint32_t edges) |
| 175 | { |
| 176 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
| 177 | struct wlsc_resize_grab *resize; |
| 178 | enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR; |
| 179 | struct wlsc_surface *es = (struct wlsc_surface *) surface; |
| 180 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 181 | /* FIXME: Reject if fullscreen */ |
| 182 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 183 | resize = malloc(sizeof *resize); |
| 184 | if (!resize) { |
| 185 | wl_client_post_no_memory(client); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | resize->grab.interface = &resize_grab_interface; |
| 190 | resize->edges = edges; |
| 191 | resize->dx = es->x - wd->input_device.grab_x; |
| 192 | resize->dy = es->y - wd->input_device.grab_y; |
| 193 | resize->width = es->width; |
| 194 | resize->height = es->height; |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 195 | resize->surface = es; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 196 | |
| 197 | if (edges == 0 || edges > 15 || |
| 198 | (edges & 3) == 3 || (edges & 12) == 12) |
| 199 | return; |
| 200 | |
| 201 | switch (edges) { |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 202 | case WL_SHELL_RESIZE_TOP: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 203 | pointer = WLSC_POINTER_TOP; |
| 204 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 205 | case WL_SHELL_RESIZE_BOTTOM: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 206 | pointer = WLSC_POINTER_BOTTOM; |
| 207 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 208 | case WL_SHELL_RESIZE_LEFT: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 209 | pointer = WLSC_POINTER_LEFT; |
| 210 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 211 | case WL_SHELL_RESIZE_TOP_LEFT: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 212 | pointer = WLSC_POINTER_TOP_LEFT; |
| 213 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 214 | case WL_SHELL_RESIZE_BOTTOM_LEFT: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 215 | pointer = WLSC_POINTER_BOTTOM_LEFT; |
| 216 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 217 | case WL_SHELL_RESIZE_RIGHT: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 218 | pointer = WLSC_POINTER_RIGHT; |
| 219 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 220 | case WL_SHELL_RESIZE_TOP_RIGHT: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 221 | pointer = WLSC_POINTER_TOP_RIGHT; |
| 222 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 223 | case WL_SHELL_RESIZE_BOTTOM_RIGHT: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 224 | pointer = WLSC_POINTER_BOTTOM_RIGHT; |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | if (wl_input_device_update_grab(&wd->input_device, |
| 229 | &resize->grab, surface, time) < 0) |
| 230 | return; |
| 231 | |
| 232 | wlsc_input_device_set_pointer_image(wd, pointer); |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 233 | wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static void |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 237 | destroy_drag(struct wl_resource *resource, struct wl_client *client) |
| 238 | { |
| 239 | struct wl_drag *drag = |
| 240 | container_of(resource, struct wl_drag, resource); |
| 241 | |
| 242 | wl_list_remove(&drag->drag_focus_listener.link); |
| 243 | if (drag->grab.input_device) |
| 244 | wl_input_device_end_grab(drag->grab.input_device, get_time()); |
| 245 | |
| 246 | free(drag); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | static void |
| 251 | wl_drag_set_pointer_focus(struct wl_drag *drag, |
| 252 | struct wl_surface *surface, uint32_t time, |
| 253 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
| 254 | { |
| 255 | char **p, **end; |
| 256 | |
| 257 | if (drag->drag_focus == surface) |
| 258 | return; |
| 259 | |
| 260 | if (drag->drag_focus && |
| 261 | (!surface || drag->drag_focus->client != surface->client)) |
| 262 | wl_client_post_event(drag->drag_focus->client, |
| 263 | &drag->drag_offer.object, |
| 264 | WL_DRAG_OFFER_POINTER_FOCUS, |
| 265 | time, NULL, 0, 0, 0, 0); |
| 266 | |
| 267 | if (surface && |
| 268 | (!drag->drag_focus || |
| 269 | drag->drag_focus->client != surface->client)) { |
| 270 | wl_client_post_global(surface->client, |
| 271 | &drag->drag_offer.object); |
| 272 | |
| 273 | end = drag->types.data + drag->types.size; |
| 274 | for (p = drag->types.data; p < end; p++) |
| 275 | wl_client_post_event(surface->client, |
| 276 | &drag->drag_offer.object, |
| 277 | WL_DRAG_OFFER_OFFER, *p); |
| 278 | } |
| 279 | |
| 280 | if (surface) { |
| 281 | wl_client_post_event(surface->client, |
| 282 | &drag->drag_offer.object, |
| 283 | WL_DRAG_OFFER_POINTER_FOCUS, |
| 284 | time, surface, |
| 285 | x, y, sx, sy); |
| 286 | |
| 287 | } |
| 288 | |
| 289 | drag->drag_focus = surface; |
| 290 | drag->pointer_focus_time = time; |
| 291 | drag->target = NULL; |
| 292 | |
| 293 | wl_list_remove(&drag->drag_focus_listener.link); |
| 294 | if (surface) |
| 295 | wl_list_insert(surface->destroy_listener_list.prev, |
| 296 | &drag->drag_focus_listener.link); |
| 297 | } |
| 298 | |
| 299 | static void |
| 300 | drag_offer_accept(struct wl_client *client, |
| 301 | struct wl_drag_offer *offer, uint32_t time, const char *type) |
| 302 | { |
| 303 | struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer); |
| 304 | char **p, **end; |
| 305 | |
| 306 | /* If the client responds to drag pointer_focus or motion |
| 307 | * events after the pointer has left the surface, we just |
| 308 | * discard the accept requests. The drag source just won't |
| 309 | * get the corresponding 'target' events and eventually the |
| 310 | * next surface/root will start sending events. */ |
| 311 | if (time < drag->pointer_focus_time) |
| 312 | return; |
| 313 | |
| 314 | drag->target = client; |
| 315 | drag->type = NULL; |
| 316 | end = drag->types.data + drag->types.size; |
| 317 | for (p = drag->types.data; p < end; p++) |
| 318 | if (type && strcmp(*p, type) == 0) |
| 319 | drag->type = *p; |
| 320 | |
| 321 | wl_client_post_event(drag->source->client, &drag->resource.object, |
| 322 | WL_DRAG_TARGET, drag->type); |
| 323 | } |
| 324 | |
| 325 | static void |
| 326 | drag_offer_receive(struct wl_client *client, |
| 327 | struct wl_drag_offer *offer, int fd) |
| 328 | { |
| 329 | struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer); |
| 330 | |
| 331 | wl_client_post_event(drag->source->client, &drag->resource.object, |
| 332 | WL_DRAG_FINISH, fd); |
| 333 | close(fd); |
| 334 | } |
| 335 | |
| 336 | static void |
| 337 | drag_offer_reject(struct wl_client *client, struct wl_drag_offer *offer) |
| 338 | { |
| 339 | struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer); |
| 340 | |
| 341 | wl_client_post_event(drag->source->client, &drag->resource.object, |
| 342 | WL_DRAG_REJECT); |
| 343 | } |
| 344 | |
| 345 | static const struct wl_drag_offer_interface drag_offer_interface = { |
| 346 | drag_offer_accept, |
| 347 | drag_offer_receive, |
| 348 | drag_offer_reject |
| 349 | }; |
| 350 | |
| 351 | static void |
| 352 | drag_offer(struct wl_client *client, struct wl_drag *drag, const char *type) |
| 353 | { |
| 354 | char **p; |
| 355 | |
| 356 | p = wl_array_add(&drag->types, sizeof *p); |
| 357 | if (p) |
| 358 | *p = strdup(type); |
| 359 | if (!p || !*p) |
| 360 | wl_client_post_no_memory(client); |
| 361 | } |
| 362 | |
| 363 | static void |
| 364 | drag_grab_motion(struct wl_grab *grab, |
| 365 | uint32_t time, int32_t x, int32_t y) |
| 366 | { |
| 367 | struct wl_drag *drag = container_of(grab, struct wl_drag, grab); |
| 368 | struct wlsc_surface *es; |
| 369 | int32_t sx, sy; |
| 370 | |
| 371 | es = pick_surface(grab->input_device, &sx, &sy); |
| 372 | wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy); |
| 373 | if (es) |
| 374 | wl_client_post_event(es->surface.client, |
| 375 | &drag->drag_offer.object, |
| 376 | WL_DRAG_OFFER_MOTION, |
| 377 | time, x, y, sx, sy); |
| 378 | } |
| 379 | |
| 380 | static void |
| 381 | drag_grab_button(struct wl_grab *grab, |
| 382 | uint32_t time, int32_t button, int32_t state) |
| 383 | { |
| 384 | } |
| 385 | |
| 386 | static void |
| 387 | drag_grab_end(struct wl_grab *grab, uint32_t time) |
| 388 | { |
| 389 | struct wl_drag *drag = container_of(grab, struct wl_drag, grab); |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 390 | struct wlsc_surface *es; |
| 391 | struct wl_input_device *device = grab->input_device; |
| 392 | int32_t sx, sy; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 393 | |
| 394 | if (drag->target) |
| 395 | wl_client_post_event(drag->target, |
| 396 | &drag->drag_offer.object, |
| 397 | WL_DRAG_OFFER_DROP); |
| 398 | |
| 399 | wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0); |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 400 | |
| 401 | es = pick_surface(grab->input_device, &sx, &sy); |
| 402 | wl_input_device_set_pointer_focus(device, |
| 403 | &es->surface, time, |
| 404 | device->x, device->y, sx, sy); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static const struct wl_grab_interface drag_grab_interface = { |
| 408 | drag_grab_motion, |
| 409 | drag_grab_button, |
| 410 | drag_grab_end |
| 411 | }; |
| 412 | |
| 413 | static void |
| 414 | drag_activate(struct wl_client *client, |
| 415 | struct wl_drag *drag, |
| 416 | struct wl_surface *surface, |
| 417 | struct wl_input_device *device, uint32_t time) |
| 418 | { |
| 419 | struct wl_display *display = wl_client_get_display (client); |
| 420 | struct wlsc_surface *target; |
| 421 | int32_t sx, sy; |
| 422 | |
| 423 | if (wl_input_device_update_grab(device, |
| 424 | &drag->grab, surface, time) < 0) |
| 425 | return; |
| 426 | |
| 427 | drag->grab.interface = &drag_grab_interface; |
| 428 | |
| 429 | drag->source = surface; |
| 430 | |
| 431 | drag->drag_offer.object.interface = &wl_drag_offer_interface; |
| 432 | drag->drag_offer.object.implementation = |
| 433 | (void (**)(void)) &drag_offer_interface; |
| 434 | |
| 435 | wl_display_add_object(display, &drag->drag_offer.object); |
| 436 | |
| 437 | target = pick_surface(device, &sx, &sy); |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 438 | wl_input_device_set_pointer_focus(device, NULL, time, 0, 0, 0, 0); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 439 | wl_drag_set_pointer_focus(drag, &target->surface, time, |
| 440 | device->x, device->y, sx, sy); |
| 441 | } |
| 442 | |
| 443 | static void |
| 444 | drag_destroy(struct wl_client *client, struct wl_drag *drag) |
| 445 | { |
| 446 | wl_resource_destroy(&drag->resource, client); |
| 447 | } |
| 448 | |
| 449 | static const struct wl_drag_interface drag_interface = { |
| 450 | drag_offer, |
| 451 | drag_activate, |
| 452 | drag_destroy, |
| 453 | }; |
| 454 | |
| 455 | static void |
| 456 | drag_handle_surface_destroy(struct wl_listener *listener, |
| 457 | struct wl_surface *surface, uint32_t time) |
| 458 | { |
| 459 | struct wl_drag *drag = |
| 460 | container_of(listener, struct wl_drag, drag_focus_listener); |
| 461 | |
| 462 | if (drag->drag_focus == surface) |
| 463 | wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0); |
| 464 | } |
| 465 | |
| 466 | static void |
| 467 | shell_create_drag(struct wl_client *client, |
| 468 | struct wl_shell *shell, uint32_t id) |
| 469 | { |
| 470 | struct wl_drag *drag; |
| 471 | |
| 472 | drag = malloc(sizeof *drag); |
| 473 | if (drag == NULL) { |
| 474 | wl_client_post_no_memory(client); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | memset(drag, 0, sizeof *drag); |
| 479 | drag->resource.object.id = id; |
| 480 | drag->resource.object.interface = &wl_drag_interface; |
| 481 | drag->resource.object.implementation = |
| 482 | (void (**)(void)) &drag_interface; |
| 483 | |
| 484 | drag->resource.destroy = destroy_drag; |
| 485 | |
| 486 | drag->drag_focus_listener.func = drag_handle_surface_destroy; |
| 487 | wl_list_init(&drag->drag_focus_listener.link); |
| 488 | |
| 489 | wl_client_add_resource(client, &drag->resource); |
| 490 | } |
| 491 | |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 492 | void |
| 493 | wlsc_selection_set_focus(struct wl_selection *selection, |
| 494 | struct wl_surface *surface, uint32_t time) |
| 495 | { |
| 496 | char **p, **end; |
| 497 | |
| 498 | if (selection->selection_focus == surface) |
| 499 | return; |
| 500 | |
| 501 | if (selection->selection_focus != NULL) |
| 502 | wl_client_post_event(selection->selection_focus->client, |
| 503 | &selection->selection_offer.object, |
| 504 | WL_SELECTION_OFFER_KEYBOARD_FOCUS, |
| 505 | NULL); |
| 506 | |
| 507 | if (surface) { |
| 508 | wl_client_post_global(surface->client, |
| 509 | &selection->selection_offer.object); |
| 510 | |
| 511 | end = selection->types.data + selection->types.size; |
| 512 | for (p = selection->types.data; p < end; p++) |
| 513 | wl_client_post_event(surface->client, |
| 514 | &selection->selection_offer.object, |
| 515 | WL_SELECTION_OFFER_OFFER, *p); |
| 516 | |
| 517 | wl_list_remove(&selection->selection_focus_listener.link); |
| 518 | wl_list_insert(surface->destroy_listener_list.prev, |
| 519 | &selection->selection_focus_listener.link); |
| 520 | |
| 521 | wl_client_post_event(surface->client, |
| 522 | &selection->selection_offer.object, |
| 523 | WL_SELECTION_OFFER_KEYBOARD_FOCUS, |
| 524 | selection->input_device); |
| 525 | } |
| 526 | |
| 527 | selection->selection_focus = surface; |
| 528 | |
| 529 | wl_list_remove(&selection->selection_focus_listener.link); |
| 530 | if (surface) |
| 531 | wl_list_insert(surface->destroy_listener_list.prev, |
| 532 | &selection->selection_focus_listener.link); |
| 533 | } |
| 534 | |
| 535 | static void |
| 536 | selection_offer_receive(struct wl_client *client, |
| 537 | struct wl_selection_offer *offer, |
| 538 | const char *mime_type, int fd) |
| 539 | { |
| 540 | struct wl_selection *selection = |
| 541 | container_of(offer, struct wl_selection, selection_offer); |
| 542 | |
| 543 | wl_client_post_event(selection->client, |
| 544 | &selection->resource.object, |
| 545 | WL_SELECTION_SEND, mime_type, fd); |
| 546 | close(fd); |
| 547 | } |
| 548 | |
| 549 | static const struct wl_selection_offer_interface selection_offer_interface = { |
| 550 | selection_offer_receive |
| 551 | }; |
| 552 | |
| 553 | static void |
| 554 | selection_offer(struct wl_client *client, |
| 555 | struct wl_selection *selection, const char *type) |
| 556 | { |
| 557 | char **p; |
| 558 | |
| 559 | p = wl_array_add(&selection->types, sizeof *p); |
| 560 | if (p) |
| 561 | *p = strdup(type); |
| 562 | if (!p || !*p) |
| 563 | wl_client_post_no_memory(client); |
| 564 | } |
| 565 | |
| 566 | static void |
| 567 | selection_activate(struct wl_client *client, |
| 568 | struct wl_selection *selection, |
| 569 | struct wl_input_device *device, uint32_t time) |
| 570 | { |
| 571 | struct wlsc_input_device *wd = (struct wlsc_input_device *) device; |
| 572 | struct wl_display *display = wl_client_get_display (client); |
| 573 | |
| 574 | selection->input_device = device; |
| 575 | |
| 576 | selection->selection_offer.object.interface = |
| 577 | &wl_selection_offer_interface; |
| 578 | selection->selection_offer.object.implementation = |
| 579 | (void (**)(void)) &selection_offer_interface; |
| 580 | |
| 581 | wl_display_add_object(display, &selection->selection_offer.object); |
| 582 | |
| 583 | if (wd->selection) { |
| 584 | wl_client_post_event(wd->selection->client, |
| 585 | &wd->selection->resource.object, |
| 586 | WL_SELECTION_CANCELLED); |
| 587 | } |
| 588 | wd->selection = selection; |
| 589 | |
| 590 | wlsc_selection_set_focus(selection, device->keyboard_focus, time); |
| 591 | } |
| 592 | |
| 593 | static void |
| 594 | selection_destroy(struct wl_client *client, struct wl_selection *selection) |
| 595 | { |
| 596 | wl_resource_destroy(&selection->resource, client); |
| 597 | } |
| 598 | |
| 599 | static const struct wl_selection_interface selection_interface = { |
| 600 | selection_offer, |
| 601 | selection_activate, |
| 602 | selection_destroy |
| 603 | }; |
| 604 | |
| 605 | static void |
| 606 | destroy_selection(struct wl_resource *resource, struct wl_client *client) |
| 607 | { |
| 608 | struct wl_selection *selection = |
| 609 | container_of(resource, struct wl_selection, resource); |
| 610 | struct wlsc_input_device *wd = |
| 611 | (struct wlsc_input_device *) selection->input_device; |
| 612 | |
| 613 | if (wd && wd->selection == selection) { |
| 614 | wd->selection = NULL; |
| 615 | wlsc_selection_set_focus(selection, NULL, get_time()); |
| 616 | } |
| 617 | |
| 618 | wl_list_remove(&selection->selection_focus_listener.link); |
| 619 | free(selection); |
| 620 | } |
| 621 | |
| 622 | static void |
| 623 | selection_handle_surface_destroy(struct wl_listener *listener, |
| 624 | struct wl_surface *surface, uint32_t time) |
| 625 | { |
| 626 | } |
| 627 | |
| 628 | static void |
| 629 | shell_create_selection(struct wl_client *client, |
| 630 | struct wl_shell *shell, uint32_t id) |
| 631 | { |
| 632 | struct wl_selection *selection; |
| 633 | |
| 634 | selection = malloc(sizeof *selection); |
| 635 | if (selection == NULL) { |
| 636 | wl_client_post_no_memory(client); |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | memset(selection, 0, sizeof *selection); |
| 641 | selection->resource.object.id = id; |
| 642 | selection->resource.object.interface = &wl_selection_interface; |
| 643 | selection->resource.object.implementation = |
| 644 | (void (**)(void)) &selection_interface; |
| 645 | |
| 646 | selection->client = client; |
| 647 | selection->resource.destroy = destroy_selection; |
| 648 | selection->selection_focus = NULL; |
| 649 | |
| 650 | selection->selection_focus_listener.func = |
| 651 | selection_handle_surface_destroy; |
| 652 | wl_list_init(&selection->selection_focus_listener.link); |
| 653 | |
| 654 | wl_client_add_resource(client, &selection->resource); |
| 655 | } |
| 656 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 657 | const static struct wl_shell_interface shell_interface = { |
| 658 | shell_move, |
| 659 | shell_resize, |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 660 | shell_create_drag, |
| 661 | shell_create_selection |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 662 | }; |
| 663 | |
| 664 | int |
| 665 | wlsc_shell_init(struct wlsc_compositor *ec) |
| 666 | { |
| 667 | struct wl_shell *shell = &ec->shell; |
| 668 | |
| 669 | shell->object.interface = &wl_shell_interface; |
| 670 | shell->object.implementation = (void (**)(void)) &shell_interface; |
| 671 | wl_display_add_object(ec->wl_display, &shell->object); |
| 672 | if (wl_display_add_global(ec->wl_display, &shell->object, NULL)) |
| 673 | return -1; |
| 674 | |
| 675 | return 0; |
| 676 | } |