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