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