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