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