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