Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Intel Corporation |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 3 | * Copyright © 2011 Collabora, Ltd. |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and |
| 6 | * its documentation for any purpose is hereby granted without fee, provided |
| 7 | * that the above copyright notice appear in all copies and that both that |
| 8 | * copyright notice and this permission notice appear in supporting |
| 9 | * documentation, and that the name of the copyright holders not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. The copyright holders make |
| 12 | * no representations about the suitability of this software for any |
| 13 | * purpose. It is provided "as is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 16 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 19 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 20 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <stdlib.h> |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 25 | #include <stdio.h> |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 26 | #include <stdbool.h> |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <unistd.h> |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 29 | #include <linux/input.h> |
Pekka Paalanen | 6cd281a | 2011-11-03 14:11:32 +0200 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <fcntl.h> |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 33 | #include <assert.h> |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 34 | |
| 35 | #include "wayland-server.h" |
| 36 | #include "compositor.h" |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 37 | #include "desktop-shell-server-protocol.h" |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 38 | |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 39 | struct wl_shell { |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 40 | struct wlsc_compositor *compositor; |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 41 | struct wlsc_shell shell; |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 42 | struct wlsc_surface *panel; |
| 43 | struct wl_listener panel_listener; |
| 44 | struct wlsc_surface *background; |
| 45 | struct wl_listener background_listener; |
Pekka Paalanen | 6cd281a | 2011-11-03 14:11:32 +0200 | [diff] [blame] | 46 | |
| 47 | struct { |
| 48 | struct wlsc_process process; |
| 49 | struct wl_client *client; |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 50 | struct wl_resource *desktop_shell; |
Pekka Paalanen | 6cd281a | 2011-11-03 14:11:32 +0200 | [diff] [blame] | 51 | } child; |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 52 | |
| 53 | bool locked; |
| 54 | bool prepare_event_sent; |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 55 | |
| 56 | struct wl_list hidden_surface_list; |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 57 | }; |
| 58 | |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 59 | struct hidden_surface { |
| 60 | struct wlsc_surface *surface; |
| 61 | struct wl_listener destroy_listener; |
| 62 | struct wl_shell *shell; |
| 63 | |
| 64 | struct wl_list link; |
| 65 | }; |
| 66 | |
| 67 | static void |
| 68 | hidden_surface_destroy(struct hidden_surface *hidden) |
| 69 | { |
| 70 | wl_list_remove(&hidden->link); |
| 71 | wl_list_remove(&hidden->destroy_listener.link); |
| 72 | free(hidden); |
| 73 | } |
| 74 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 75 | struct wlsc_move_grab { |
| 76 | struct wl_grab grab; |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 77 | struct wlsc_surface *surface; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 78 | int32_t dx, dy; |
| 79 | }; |
| 80 | |
| 81 | static void |
| 82 | move_grab_motion(struct wl_grab *grab, |
| 83 | uint32_t time, int32_t x, int32_t y) |
| 84 | { |
| 85 | struct wlsc_move_grab *move = (struct wlsc_move_grab *) grab; |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 86 | struct wlsc_surface *es = move->surface; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 87 | |
Kristian Høgsberg | a691aee | 2011-06-23 21:43:50 -0400 | [diff] [blame] | 88 | wlsc_surface_configure(es, x + move->dx, y + move->dy, |
| 89 | es->width, es->height); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static void |
| 93 | move_grab_button(struct wl_grab *grab, |
| 94 | uint32_t time, int32_t button, int32_t state) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | move_grab_end(struct wl_grab *grab, uint32_t time) |
| 100 | { |
| 101 | free(grab); |
| 102 | } |
| 103 | |
| 104 | static const struct wl_grab_interface move_grab_interface = { |
| 105 | move_grab_motion, |
| 106 | move_grab_button, |
| 107 | move_grab_end |
| 108 | }; |
| 109 | |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 110 | static int |
| 111 | wlsc_surface_move(struct wlsc_surface *es, |
| 112 | struct wlsc_input_device *wd, uint32_t time) |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 113 | { |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 114 | struct wlsc_move_grab *move; |
| 115 | |
| 116 | move = malloc(sizeof *move); |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 117 | if (!move) |
| 118 | return -1; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 119 | |
| 120 | move->grab.interface = &move_grab_interface; |
| 121 | move->dx = es->x - wd->input_device.grab_x; |
| 122 | move->dy = es->y - wd->input_device.grab_y; |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 123 | move->surface = es; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 124 | |
| 125 | if (wl_input_device_update_grab(&wd->input_device, |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 126 | &move->grab, &es->surface, time) < 0) |
| 127 | return 0; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 128 | |
| 129 | wlsc_input_device_set_pointer_image(wd, WLSC_POINTER_DRAGGING); |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 130 | wl_input_device_set_pointer_focus(&wd->input_device, |
| 131 | NULL, time, 0, 0, 0, 0); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | shell_move(struct wl_client *client, struct wl_resource *resource, |
| 138 | struct wl_resource *surface_resource, |
| 139 | struct wl_resource *input_resource, uint32_t time) |
| 140 | { |
| 141 | struct wlsc_input_device *wd = input_resource->data; |
| 142 | struct wlsc_surface *es = surface_resource->data; |
| 143 | |
| 144 | if (wlsc_surface_move(es, wd, time) < 0) |
Kristian Høgsberg | 9ebcf94 | 2011-09-01 09:54:57 -0400 | [diff] [blame] | 145 | wl_resource_post_no_memory(resource); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | struct wlsc_resize_grab { |
| 149 | struct wl_grab grab; |
| 150 | uint32_t edges; |
| 151 | int32_t dx, dy, width, height; |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 152 | struct wlsc_surface *surface; |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 153 | struct wl_resource *resource; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | static void |
| 157 | resize_grab_motion(struct wl_grab *grab, |
| 158 | uint32_t time, int32_t x, int32_t y) |
| 159 | { |
| 160 | struct wlsc_resize_grab *resize = (struct wlsc_resize_grab *) grab; |
| 161 | struct wl_input_device *device = grab->input_device; |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 162 | struct wl_surface *surface = &resize->surface->surface; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 163 | int32_t width, height; |
| 164 | |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 165 | if (resize->edges & WL_SHELL_RESIZE_LEFT) { |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 166 | width = device->grab_x - x + resize->width; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 167 | } else if (resize->edges & WL_SHELL_RESIZE_RIGHT) { |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 168 | width = x - device->grab_x + resize->width; |
| 169 | } else { |
| 170 | width = resize->width; |
| 171 | } |
| 172 | |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 173 | if (resize->edges & WL_SHELL_RESIZE_TOP) { |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 174 | height = device->grab_y - y + resize->height; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 175 | } else if (resize->edges & WL_SHELL_RESIZE_BOTTOM) { |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 176 | height = y - device->grab_y + resize->height; |
| 177 | } else { |
| 178 | height = resize->height; |
| 179 | } |
| 180 | |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 181 | wl_resource_post_event(resize->resource, |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 182 | WL_SHELL_CONFIGURE, time, resize->edges, |
| 183 | surface, width, height); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static void |
| 187 | resize_grab_button(struct wl_grab *grab, |
| 188 | uint32_t time, int32_t button, int32_t state) |
| 189 | { |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | resize_grab_end(struct wl_grab *grab, uint32_t time) |
| 194 | { |
| 195 | free(grab); |
| 196 | } |
| 197 | |
| 198 | static const struct wl_grab_interface resize_grab_interface = { |
| 199 | resize_grab_motion, |
| 200 | resize_grab_button, |
| 201 | resize_grab_end |
| 202 | }; |
| 203 | |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 204 | static int |
| 205 | wlsc_surface_resize(struct wlsc_surface *es, |
| 206 | struct wlsc_input_device *wd, |
| 207 | uint32_t time, uint32_t edges, |
| 208 | struct wl_resource *resource) |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 209 | { |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 210 | struct wlsc_resize_grab *resize; |
| 211 | enum wlsc_pointer_type pointer = WLSC_POINTER_LEFT_PTR; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 212 | |
Kristian Høgsberg | 0ce2457 | 2011-01-28 15:18:33 -0500 | [diff] [blame] | 213 | /* FIXME: Reject if fullscreen */ |
| 214 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 215 | resize = malloc(sizeof *resize); |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 216 | if (!resize) |
| 217 | return -1; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 218 | |
| 219 | resize->grab.interface = &resize_grab_interface; |
| 220 | resize->edges = edges; |
| 221 | resize->dx = es->x - wd->input_device.grab_x; |
| 222 | resize->dy = es->y - wd->input_device.grab_y; |
| 223 | resize->width = es->width; |
| 224 | resize->height = es->height; |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 225 | resize->surface = es; |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 226 | resize->resource = resource; |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 227 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 228 | if (edges == 0 || edges > 15 || |
| 229 | (edges & 3) == 3 || (edges & 12) == 12) |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 230 | return 0; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 231 | |
| 232 | switch (edges) { |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 233 | case WL_SHELL_RESIZE_TOP: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 234 | pointer = WLSC_POINTER_TOP; |
| 235 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 236 | case WL_SHELL_RESIZE_BOTTOM: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 237 | pointer = WLSC_POINTER_BOTTOM; |
| 238 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 239 | case WL_SHELL_RESIZE_LEFT: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 240 | pointer = WLSC_POINTER_LEFT; |
| 241 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 242 | case WL_SHELL_RESIZE_TOP_LEFT: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 243 | pointer = WLSC_POINTER_TOP_LEFT; |
| 244 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 245 | case WL_SHELL_RESIZE_BOTTOM_LEFT: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 246 | pointer = WLSC_POINTER_BOTTOM_LEFT; |
| 247 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 248 | case WL_SHELL_RESIZE_RIGHT: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 249 | pointer = WLSC_POINTER_RIGHT; |
| 250 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 251 | case WL_SHELL_RESIZE_TOP_RIGHT: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 252 | pointer = WLSC_POINTER_TOP_RIGHT; |
| 253 | break; |
Kristian Høgsberg | 027931b | 2011-01-21 21:57:55 -0500 | [diff] [blame] | 254 | case WL_SHELL_RESIZE_BOTTOM_RIGHT: |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 255 | pointer = WLSC_POINTER_BOTTOM_RIGHT; |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | if (wl_input_device_update_grab(&wd->input_device, |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 260 | &resize->grab, &es->surface, time) < 0) |
| 261 | return 0; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 262 | |
| 263 | wlsc_input_device_set_pointer_image(wd, pointer); |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 264 | wl_input_device_set_pointer_focus(&wd->input_device, |
| 265 | NULL, time, 0, 0, 0, 0); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static void |
| 271 | shell_resize(struct wl_client *client, struct wl_resource *resource, |
| 272 | struct wl_resource *surface_resource, |
| 273 | struct wl_resource *input_resource, uint32_t time, uint32_t edges) |
| 274 | { |
| 275 | struct wlsc_input_device *wd = input_resource->data; |
| 276 | struct wlsc_surface *es = surface_resource->data; |
| 277 | |
| 278 | /* FIXME: Reject if fullscreen */ |
| 279 | |
| 280 | if (wlsc_surface_resize(es, wd, time, edges, resource) < 0) |
Kristian Høgsberg | 9ebcf94 | 2011-09-01 09:54:57 -0400 | [diff] [blame] | 281 | wl_resource_post_no_memory(resource); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static void |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 285 | shell_set_toplevel(struct wl_client *client, |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 286 | struct wl_resource *resource, |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 287 | struct wl_resource *surface_resource) |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 288 | |
| 289 | { |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 290 | struct wlsc_surface *es = surface_resource->data; |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 291 | |
| 292 | if (es->map_type == WLSC_SURFACE_MAP_FULLSCREEN) { |
| 293 | es->x = es->saved_x; |
| 294 | es->y = es->saved_y; |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | wlsc_surface_damage(es); |
| 298 | es->map_type = WLSC_SURFACE_MAP_TOPLEVEL; |
| 299 | es->fullscreen_output = NULL; |
| 300 | } |
| 301 | |
| 302 | static void |
| 303 | shell_set_transient(struct wl_client *client, |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 304 | struct wl_resource *resource, |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 305 | struct wl_resource *surface_resource, |
| 306 | struct wl_resource *parent_resource, |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 307 | int x, int y, uint32_t flags) |
| 308 | { |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 309 | struct wlsc_surface *es = surface_resource->data; |
| 310 | struct wlsc_surface *pes = parent_resource->data; |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 311 | |
| 312 | /* assign to parents output */ |
| 313 | es->output = pes->output; |
| 314 | |
| 315 | es->x = pes->x + x; |
| 316 | es->y = pes->y + y; |
| 317 | |
| 318 | wlsc_surface_damage(es); |
| 319 | es->map_type = WLSC_SURFACE_MAP_TRANSIENT; |
| 320 | } |
| 321 | |
| 322 | static void |
| 323 | shell_set_fullscreen(struct wl_client *client, |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 324 | struct wl_resource *resource, |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 325 | struct wl_resource *surface_resource) |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 326 | |
| 327 | { |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 328 | struct wlsc_surface *es = surface_resource->data; |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 329 | struct wlsc_output *output; |
| 330 | |
| 331 | /* FIXME: Fullscreen on first output */ |
| 332 | /* FIXME: Handle output going away */ |
| 333 | output = container_of(es->compositor->output_list.next, |
| 334 | struct wlsc_output, link); |
| 335 | es->output = output; |
| 336 | |
| 337 | es->saved_x = es->x; |
| 338 | es->saved_y = es->y; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 339 | es->x = (output->current->width - es->width) / 2; |
| 340 | es->y = (output->current->height - es->height) / 2; |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 341 | es->fullscreen_output = output; |
| 342 | wlsc_surface_damage(es); |
| 343 | es->map_type = WLSC_SURFACE_MAP_FULLSCREEN; |
| 344 | } |
| 345 | |
| 346 | static void |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 347 | destroy_drag(struct wl_resource *resource) |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 348 | { |
| 349 | struct wl_drag *drag = |
| 350 | container_of(resource, struct wl_drag, resource); |
| 351 | |
| 352 | wl_list_remove(&drag->drag_focus_listener.link); |
| 353 | if (drag->grab.input_device) |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 354 | wl_input_device_end_grab(drag->grab.input_device, |
| 355 | wlsc_compositor_get_time()); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 356 | |
| 357 | free(drag); |
| 358 | } |
| 359 | |
| 360 | |
| 361 | static void |
| 362 | wl_drag_set_pointer_focus(struct wl_drag *drag, |
| 363 | struct wl_surface *surface, uint32_t time, |
| 364 | int32_t x, int32_t y, int32_t sx, int32_t sy) |
| 365 | { |
| 366 | char **p, **end; |
| 367 | |
| 368 | if (drag->drag_focus == surface) |
| 369 | return; |
| 370 | |
| 371 | if (drag->drag_focus && |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 372 | (!surface || |
| 373 | drag->drag_focus->resource.client != surface->resource.client)) |
| 374 | wl_resource_post_event(&drag->drag_offer.resource, |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 375 | WL_DRAG_OFFER_POINTER_FOCUS, |
| 376 | time, NULL, 0, 0, 0, 0); |
| 377 | |
| 378 | if (surface && |
| 379 | (!drag->drag_focus || |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 380 | drag->drag_focus->resource.client != surface->resource.client)) { |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 381 | |
| 382 | drag->drag_offer.resource.client = surface->resource.client; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 383 | end = drag->types.data + drag->types.size; |
| 384 | for (p = drag->types.data; p < end; p++) |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 385 | wl_resource_post_event(&drag->drag_offer.resource, |
| 386 | WL_DRAG_OFFER_OFFER, *p); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | if (surface) { |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 390 | wl_resource_post_event(&drag->drag_offer.resource, |
| 391 | WL_DRAG_OFFER_POINTER_FOCUS, |
| 392 | time, surface, |
| 393 | x, y, sx, sy); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 394 | |
| 395 | } |
| 396 | |
| 397 | drag->drag_focus = surface; |
| 398 | drag->pointer_focus_time = time; |
| 399 | drag->target = NULL; |
| 400 | |
| 401 | wl_list_remove(&drag->drag_focus_listener.link); |
| 402 | if (surface) |
Benjamin Franzke | 4721a3c | 2011-05-06 17:13:17 +0200 | [diff] [blame] | 403 | wl_list_insert(surface->resource.destroy_listener_list.prev, |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 404 | &drag->drag_focus_listener.link); |
| 405 | } |
| 406 | |
| 407 | static void |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 408 | drag_offer_accept(struct wl_client *client, struct wl_resource *resource, |
| 409 | uint32_t time, const char *type) |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 410 | { |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 411 | struct wl_drag_offer *offer = resource->data; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 412 | struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer); |
| 413 | char **p, **end; |
| 414 | |
| 415 | /* If the client responds to drag pointer_focus or motion |
| 416 | * events after the pointer has left the surface, we just |
| 417 | * discard the accept requests. The drag source just won't |
| 418 | * get the corresponding 'target' events and eventually the |
| 419 | * next surface/root will start sending events. */ |
| 420 | if (time < drag->pointer_focus_time) |
| 421 | return; |
| 422 | |
| 423 | drag->target = client; |
| 424 | drag->type = NULL; |
| 425 | end = drag->types.data + drag->types.size; |
| 426 | for (p = drag->types.data; p < end; p++) |
| 427 | if (type && strcmp(*p, type) == 0) |
| 428 | drag->type = *p; |
| 429 | |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 430 | wl_resource_post_event(&drag->resource, WL_DRAG_TARGET, drag->type); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static void |
| 434 | drag_offer_receive(struct wl_client *client, |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 435 | struct wl_resource *resource, int fd) |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 436 | { |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 437 | struct wl_drag_offer *offer = resource->data; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 438 | struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer); |
| 439 | |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 440 | wl_resource_post_event(&drag->resource, WL_DRAG_FINISH, fd); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 441 | close(fd); |
| 442 | } |
| 443 | |
| 444 | static void |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 445 | drag_offer_reject(struct wl_client *client, struct wl_resource *resource) |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 446 | { |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 447 | struct wl_drag_offer *offer = resource->data; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 448 | struct wl_drag *drag = container_of(offer, struct wl_drag, drag_offer); |
| 449 | |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 450 | wl_resource_post_event(&drag->resource, WL_DRAG_REJECT); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static const struct wl_drag_offer_interface drag_offer_interface = { |
| 454 | drag_offer_accept, |
| 455 | drag_offer_receive, |
| 456 | drag_offer_reject |
| 457 | }; |
| 458 | |
| 459 | static void |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 460 | drag_offer(struct wl_client *client, |
| 461 | struct wl_resource *resource, const char *type) |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 462 | { |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 463 | struct wl_drag *drag = resource->data; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 464 | char **p; |
| 465 | |
| 466 | p = wl_array_add(&drag->types, sizeof *p); |
| 467 | if (p) |
| 468 | *p = strdup(type); |
| 469 | if (!p || !*p) |
Kristian Høgsberg | 9ebcf94 | 2011-09-01 09:54:57 -0400 | [diff] [blame] | 470 | wl_resource_post_no_memory(resource); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static void |
| 474 | drag_grab_motion(struct wl_grab *grab, |
| 475 | uint32_t time, int32_t x, int32_t y) |
| 476 | { |
| 477 | struct wl_drag *drag = container_of(grab, struct wl_drag, grab); |
| 478 | struct wlsc_surface *es; |
| 479 | int32_t sx, sy; |
| 480 | |
| 481 | es = pick_surface(grab->input_device, &sx, &sy); |
| 482 | wl_drag_set_pointer_focus(drag, &es->surface, time, x, y, sx, sy); |
| 483 | if (es) |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 484 | wl_resource_post_event(&drag->drag_offer.resource, |
| 485 | WL_DRAG_OFFER_MOTION, |
| 486 | time, x, y, sx, sy); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static void |
| 490 | drag_grab_button(struct wl_grab *grab, |
| 491 | uint32_t time, int32_t button, int32_t state) |
| 492 | { |
| 493 | } |
| 494 | |
| 495 | static void |
| 496 | drag_grab_end(struct wl_grab *grab, uint32_t time) |
| 497 | { |
| 498 | struct wl_drag *drag = container_of(grab, struct wl_drag, grab); |
| 499 | |
| 500 | if (drag->target) |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 501 | wl_resource_post_event(&drag->drag_offer.resource, |
| 502 | WL_DRAG_OFFER_DROP); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 503 | |
| 504 | wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0); |
| 505 | } |
| 506 | |
| 507 | static const struct wl_grab_interface drag_grab_interface = { |
| 508 | drag_grab_motion, |
| 509 | drag_grab_button, |
| 510 | drag_grab_end |
| 511 | }; |
| 512 | |
| 513 | static void |
| 514 | drag_activate(struct wl_client *client, |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 515 | struct wl_resource *resource, |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 516 | struct wl_resource *surface_resource, |
| 517 | struct wl_resource *device_resource, uint32_t time) |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 518 | { |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 519 | struct wl_drag *drag = resource->data; |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 520 | struct wl_surface *surface = surface_resource->data; |
| 521 | struct wl_input_device *device = device_resource->data; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 522 | struct wl_display *display = wl_client_get_display (client); |
| 523 | struct wlsc_surface *target; |
| 524 | int32_t sx, sy; |
| 525 | |
| 526 | if (wl_input_device_update_grab(device, |
| 527 | &drag->grab, surface, time) < 0) |
| 528 | return; |
| 529 | |
| 530 | drag->grab.interface = &drag_grab_interface; |
| 531 | |
| 532 | drag->source = surface; |
| 533 | |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 534 | drag->drag_offer.resource.object.interface = &wl_drag_offer_interface; |
| 535 | drag->drag_offer.resource.object.implementation = |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 536 | (void (**)(void)) &drag_offer_interface; |
| 537 | |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 538 | wl_display_add_global(display, &wl_drag_offer_interface, drag, NULL); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 539 | |
| 540 | target = pick_surface(device, &sx, &sy); |
Kristian Høgsberg | dd4046a | 2011-01-21 17:00:09 -0500 | [diff] [blame] | 541 | 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] | 542 | wl_drag_set_pointer_focus(drag, &target->surface, time, |
| 543 | device->x, device->y, sx, sy); |
| 544 | } |
| 545 | |
| 546 | static void |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 547 | drag_destroy(struct wl_client *client, struct wl_resource *resource) |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 548 | { |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 549 | wl_resource_destroy(resource, wlsc_compositor_get_time()); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | static const struct wl_drag_interface drag_interface = { |
| 553 | drag_offer, |
| 554 | drag_activate, |
| 555 | drag_destroy, |
| 556 | }; |
| 557 | |
| 558 | static void |
| 559 | drag_handle_surface_destroy(struct wl_listener *listener, |
Benjamin Franzke | 4721a3c | 2011-05-06 17:13:17 +0200 | [diff] [blame] | 560 | struct wl_resource *resource, uint32_t time) |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 561 | { |
| 562 | struct wl_drag *drag = |
| 563 | container_of(listener, struct wl_drag, drag_focus_listener); |
Benjamin Franzke | 4721a3c | 2011-05-06 17:13:17 +0200 | [diff] [blame] | 564 | struct wl_surface *surface = (struct wl_surface *) resource; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 565 | |
| 566 | if (drag->drag_focus == surface) |
| 567 | wl_drag_set_pointer_focus(drag, NULL, time, 0, 0, 0, 0); |
| 568 | } |
| 569 | |
| 570 | static void |
| 571 | shell_create_drag(struct wl_client *client, |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 572 | struct wl_resource *resource, uint32_t id) |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 573 | { |
| 574 | struct wl_drag *drag; |
| 575 | |
| 576 | drag = malloc(sizeof *drag); |
| 577 | if (drag == NULL) { |
Kristian Høgsberg | 9ebcf94 | 2011-09-01 09:54:57 -0400 | [diff] [blame] | 578 | wl_resource_post_no_memory(resource); |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 579 | return; |
| 580 | } |
| 581 | |
| 582 | memset(drag, 0, sizeof *drag); |
| 583 | drag->resource.object.id = id; |
| 584 | drag->resource.object.interface = &wl_drag_interface; |
| 585 | drag->resource.object.implementation = |
| 586 | (void (**)(void)) &drag_interface; |
| 587 | |
Pekka Paalanen | 02ebfb1 | 2011-10-24 17:34:53 +0300 | [diff] [blame] | 588 | drag->resource.data = drag; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 589 | drag->resource.destroy = destroy_drag; |
| 590 | |
| 591 | drag->drag_focus_listener.func = drag_handle_surface_destroy; |
| 592 | wl_list_init(&drag->drag_focus_listener.link); |
| 593 | |
| 594 | wl_client_add_resource(client, &drag->resource); |
| 595 | } |
| 596 | |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 597 | static void |
| 598 | wlsc_selection_set_focus(struct wlsc_shell *shell, |
| 599 | struct wl_selection *selection, |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 600 | struct wl_surface *surface, uint32_t time) |
| 601 | { |
| 602 | char **p, **end; |
| 603 | |
| 604 | if (selection->selection_focus == surface) |
| 605 | return; |
| 606 | |
| 607 | if (selection->selection_focus != NULL) |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 608 | wl_resource_post_event(&selection->selection_offer.resource, |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 609 | WL_SELECTION_OFFER_KEYBOARD_FOCUS, |
| 610 | NULL); |
| 611 | |
| 612 | if (surface) { |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 613 | |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 614 | selection->selection_offer.resource.client = surface->resource.client; |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 615 | end = selection->types.data + selection->types.size; |
| 616 | for (p = selection->types.data; p < end; p++) |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 617 | wl_resource_post_event(&selection->selection_offer.resource, |
| 618 | WL_SELECTION_OFFER_OFFER, *p); |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 619 | |
| 620 | wl_list_remove(&selection->selection_focus_listener.link); |
Benjamin Franzke | 4721a3c | 2011-05-06 17:13:17 +0200 | [diff] [blame] | 621 | wl_list_insert(surface->resource.destroy_listener_list.prev, |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 622 | &selection->selection_focus_listener.link); |
| 623 | |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 624 | wl_resource_post_event(&selection->selection_offer.resource, |
| 625 | WL_SELECTION_OFFER_KEYBOARD_FOCUS, |
| 626 | selection->input_device); |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | selection->selection_focus = surface; |
| 630 | |
| 631 | wl_list_remove(&selection->selection_focus_listener.link); |
| 632 | if (surface) |
Benjamin Franzke | 4721a3c | 2011-05-06 17:13:17 +0200 | [diff] [blame] | 633 | wl_list_insert(surface->resource.destroy_listener_list.prev, |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 634 | &selection->selection_focus_listener.link); |
| 635 | } |
| 636 | |
| 637 | static void |
| 638 | selection_offer_receive(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 | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 640 | const char *mime_type, int fd) |
| 641 | { |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 642 | struct wl_selection_offer *offer = resource->data; |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 643 | struct wl_selection *selection = |
| 644 | container_of(offer, struct wl_selection, selection_offer); |
| 645 | |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 646 | wl_resource_post_event(&selection->resource, |
| 647 | WL_SELECTION_SEND, mime_type, fd); |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 648 | close(fd); |
| 649 | } |
| 650 | |
| 651 | static const struct wl_selection_offer_interface selection_offer_interface = { |
| 652 | selection_offer_receive |
| 653 | }; |
| 654 | |
| 655 | static void |
| 656 | selection_offer(struct wl_client *client, |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 657 | struct wl_resource *resource, const char *type) |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 658 | { |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 659 | struct wl_selection *selection = resource->data; |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 660 | char **p; |
| 661 | |
| 662 | p = wl_array_add(&selection->types, sizeof *p); |
| 663 | if (p) |
| 664 | *p = strdup(type); |
| 665 | if (!p || !*p) |
Kristian Høgsberg | 9ebcf94 | 2011-09-01 09:54:57 -0400 | [diff] [blame] | 666 | wl_resource_post_no_memory(resource); |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | static void |
| 670 | selection_activate(struct wl_client *client, |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 671 | struct wl_resource *resource, |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 672 | struct wl_resource *input_resource, uint32_t time) |
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 | struct wl_selection *selection = resource->data; |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 675 | struct wlsc_input_device *wd = input_resource->data; |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 676 | struct wl_display *display = wl_client_get_display (client); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 677 | struct wlsc_compositor *compositor = |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 678 | (struct wlsc_compositor *) wd->input_device.compositor; |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 679 | |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 680 | selection->input_device = &wd->input_device; |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 681 | |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 682 | selection->selection_offer.resource.object.interface = |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 683 | &wl_selection_offer_interface; |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 684 | selection->selection_offer.resource.object.implementation = |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 685 | (void (**)(void)) &selection_offer_interface; |
| 686 | |
Kristian Høgsberg | d9551a3 | 2011-08-19 12:07:44 -0400 | [diff] [blame] | 687 | wl_display_add_global(display, |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 688 | &wl_selection_offer_interface, selection, NULL); |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 689 | |
| 690 | if (wd->selection) { |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 691 | wl_resource_post_event(&wd->selection->resource, |
| 692 | WL_SELECTION_CANCELLED); |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 693 | } |
| 694 | wd->selection = selection; |
| 695 | |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 696 | wlsc_selection_set_focus(compositor->shell, selection, |
| 697 | wd->input_device.keyboard_focus, time); |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | static void |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 701 | selection_destroy(struct wl_client *client, struct wl_resource *resource) |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 702 | { |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 703 | wl_resource_destroy(resource, wlsc_compositor_get_time()); |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static const struct wl_selection_interface selection_interface = { |
| 707 | selection_offer, |
| 708 | selection_activate, |
| 709 | selection_destroy |
| 710 | }; |
| 711 | |
| 712 | static void |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 713 | destroy_selection(struct wl_resource *resource) |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 714 | { |
| 715 | struct wl_selection *selection = |
| 716 | container_of(resource, struct wl_selection, resource); |
| 717 | struct wlsc_input_device *wd = |
| 718 | (struct wlsc_input_device *) selection->input_device; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 719 | struct wlsc_compositor *compositor = |
| 720 | (struct wlsc_compositor *) wd->input_device.compositor; |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 721 | |
| 722 | if (wd && wd->selection == selection) { |
| 723 | wd->selection = NULL; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 724 | wlsc_selection_set_focus(compositor->shell, |
| 725 | selection, NULL, |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 726 | wlsc_compositor_get_time()); |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | wl_list_remove(&selection->selection_focus_listener.link); |
| 730 | free(selection); |
| 731 | } |
| 732 | |
| 733 | static void |
| 734 | selection_handle_surface_destroy(struct wl_listener *listener, |
Benjamin Franzke | 4721a3c | 2011-05-06 17:13:17 +0200 | [diff] [blame] | 735 | struct wl_resource *resource, uint32_t time) |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 736 | { |
| 737 | } |
| 738 | |
| 739 | static void |
| 740 | shell_create_selection(struct wl_client *client, |
Kristian Høgsberg | 904055a | 2011-08-18 17:55:30 -0400 | [diff] [blame] | 741 | struct wl_resource *resource, uint32_t id) |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 742 | { |
| 743 | struct wl_selection *selection; |
| 744 | |
| 745 | selection = malloc(sizeof *selection); |
| 746 | if (selection == NULL) { |
Kristian Høgsberg | 9ebcf94 | 2011-09-01 09:54:57 -0400 | [diff] [blame] | 747 | wl_resource_post_no_memory(resource); |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 748 | return; |
| 749 | } |
| 750 | |
| 751 | memset(selection, 0, sizeof *selection); |
| 752 | selection->resource.object.id = id; |
| 753 | selection->resource.object.interface = &wl_selection_interface; |
| 754 | selection->resource.object.implementation = |
| 755 | (void (**)(void)) &selection_interface; |
| 756 | |
| 757 | selection->client = client; |
| 758 | selection->resource.destroy = destroy_selection; |
| 759 | selection->selection_focus = NULL; |
| 760 | |
| 761 | selection->selection_focus_listener.func = |
| 762 | selection_handle_surface_destroy; |
| 763 | wl_list_init(&selection->selection_focus_listener.link); |
| 764 | |
| 765 | wl_client_add_resource(client, &selection->resource); |
| 766 | } |
| 767 | |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 768 | static const struct wl_shell_interface shell_interface = { |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 769 | shell_move, |
| 770 | shell_resize, |
Kristian Høgsberg | ae6c8a6 | 2011-01-18 09:08:53 -0500 | [diff] [blame] | 771 | shell_create_drag, |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 772 | shell_create_selection, |
| 773 | shell_set_toplevel, |
| 774 | shell_set_transient, |
| 775 | shell_set_fullscreen |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 776 | }; |
| 777 | |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 778 | static void |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 779 | handle_background_surface_destroy(struct wl_listener *listener, |
| 780 | struct wl_resource *resource, uint32_t time) |
| 781 | { |
| 782 | struct wl_shell *shell = |
| 783 | container_of(listener, struct wl_shell, background_listener); |
| 784 | |
| 785 | fprintf(stderr, "background surface gone\n"); |
| 786 | shell->background = NULL; |
| 787 | } |
| 788 | |
| 789 | static void |
| 790 | desktop_shell_set_background(struct wl_client *client, |
| 791 | struct wl_resource *resource, |
| 792 | struct wl_resource *surface_resource) |
| 793 | { |
| 794 | struct wl_shell *shell = resource->data; |
| 795 | struct wlsc_surface *surface = surface_resource->data; |
| 796 | struct wlsc_output *output = |
| 797 | container_of(shell->compositor->output_list.next, |
| 798 | struct wlsc_output, link); |
| 799 | |
| 800 | shell->background = surface_resource->data; |
| 801 | shell->background_listener.func = handle_background_surface_destroy; |
| 802 | wl_list_insert(&surface_resource->destroy_listener_list, |
| 803 | &shell->background_listener.link); |
| 804 | |
| 805 | wl_resource_post_event(resource, |
| 806 | DESKTOP_SHELL_CONFIGURE, |
| 807 | wlsc_compositor_get_time(), 0, surface, |
| 808 | output->current->width, |
| 809 | output->current->height); |
| 810 | } |
| 811 | |
| 812 | static void |
| 813 | handle_panel_surface_destroy(struct wl_listener *listener, |
| 814 | struct wl_resource *resource, uint32_t time) |
| 815 | { |
| 816 | struct wl_shell *shell = |
| 817 | container_of(listener, struct wl_shell, panel_listener); |
| 818 | |
| 819 | fprintf(stderr, "panel surface gone\n"); |
| 820 | shell->panel = NULL; |
| 821 | } |
| 822 | |
| 823 | static void |
| 824 | desktop_shell_set_panel(struct wl_client *client, |
| 825 | struct wl_resource *resource, |
| 826 | struct wl_resource *surface_resource) |
| 827 | { |
| 828 | struct wl_shell *shell = resource->data; |
| 829 | struct wlsc_output *output = |
| 830 | container_of(shell->compositor->output_list.next, |
| 831 | struct wlsc_output, link); |
| 832 | |
| 833 | shell->panel = surface_resource->data; |
| 834 | |
| 835 | shell->panel_listener.func = handle_panel_surface_destroy; |
| 836 | wl_list_insert(&surface_resource->destroy_listener_list, |
| 837 | &shell->panel_listener.link); |
| 838 | |
| 839 | wl_resource_post_event(resource, |
| 840 | DESKTOP_SHELL_CONFIGURE, |
| 841 | wlsc_compositor_get_time(), 0, surface_resource, |
| 842 | output->current->width, |
| 843 | output->current->height); |
| 844 | } |
| 845 | |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 846 | static void |
| 847 | desktop_shell_set_lock_surface(struct wl_client *client, |
| 848 | struct wl_resource *resource, |
| 849 | struct wl_resource *surface_resource) |
| 850 | { |
Kristian Høgsberg | af867cc | 2011-11-15 13:34:49 +0200 | [diff] [blame] | 851 | struct wl_shell *shell = resource->data; |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 852 | struct wlsc_surface *es = surface_resource->data; |
Kristian Høgsberg | af867cc | 2011-11-15 13:34:49 +0200 | [diff] [blame] | 853 | |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 854 | shell->prepare_event_sent = false; |
Kristian Høgsberg | af867cc | 2011-11-15 13:34:49 +0200 | [diff] [blame] | 855 | |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 856 | if (!shell->locked) |
| 857 | return; |
| 858 | |
| 859 | wl_list_remove(&es->link); |
| 860 | wl_list_insert(&shell->compositor->surface_list, &es->link); |
| 861 | |
| 862 | wlsc_compositor_repick(shell->compositor); |
| 863 | wlsc_compositor_wake(shell->compositor); |
| 864 | } |
| 865 | |
| 866 | static void |
| 867 | resume_desktop(struct wl_shell *shell) |
| 868 | { |
| 869 | struct hidden_surface *hidden; |
| 870 | struct hidden_surface *tmp; |
| 871 | struct wl_list *elem; |
| 872 | struct wl_list *put = &shell->compositor->surface_list; |
| 873 | |
| 874 | wl_list_for_each_safe(hidden, tmp, &shell->hidden_surface_list, link) { |
| 875 | elem = &hidden->surface->link; |
| 876 | wl_list_remove(elem); |
| 877 | |
| 878 | if (hidden->surface == shell->panel) { |
| 879 | wl_list_insert(&shell->compositor->surface_list, elem); |
| 880 | if (put == &shell->compositor->surface_list) |
| 881 | put = elem; |
| 882 | } else { |
| 883 | wl_list_insert(put, elem); |
| 884 | put = elem; |
| 885 | } |
| 886 | |
| 887 | hidden_surface_destroy(hidden); |
| 888 | } |
| 889 | |
| 890 | if (!wl_list_empty(&shell->hidden_surface_list)) { |
| 891 | fprintf(stderr, |
| 892 | "%s: Assertion failed: hidden_surface_list is not empty.\n", |
| 893 | __func__); |
| 894 | } |
| 895 | |
| 896 | shell->locked = false; |
| 897 | wlsc_compositor_repick(shell->compositor); |
| 898 | wlsc_compositor_damage_all(shell->compositor); |
Kristian Høgsberg | af867cc | 2011-11-15 13:34:49 +0200 | [diff] [blame] | 899 | wlsc_compositor_wake(shell->compositor); |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | static void |
| 903 | desktop_shell_unlock(struct wl_client *client, |
| 904 | struct wl_resource *resource) |
| 905 | { |
| 906 | struct wl_shell *shell = resource->data; |
| 907 | |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 908 | shell->prepare_event_sent = false; |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 909 | |
| 910 | if (shell->locked) |
| 911 | resume_desktop(shell); |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 912 | } |
| 913 | |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 914 | static const struct desktop_shell_interface desktop_shell_implementation = { |
| 915 | desktop_shell_set_background, |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 916 | desktop_shell_set_panel, |
| 917 | desktop_shell_set_lock_surface, |
| 918 | desktop_shell_unlock |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 919 | }; |
| 920 | |
| 921 | static void |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 922 | move_binding(struct wl_input_device *device, uint32_t time, |
| 923 | uint32_t key, uint32_t button, uint32_t state, void *data) |
| 924 | { |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 925 | struct wl_shell *shell = data; |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 926 | struct wlsc_surface *surface = |
| 927 | (struct wlsc_surface *) device->pointer_focus; |
| 928 | |
Kristian Høgsberg | a4a42f0 | 2011-09-08 16:56:57 -0400 | [diff] [blame] | 929 | if (surface == NULL || |
| 930 | surface->map_type == WLSC_SURFACE_MAP_FULLSCREEN) |
Kristian Høgsberg | 10f097e | 2011-04-13 11:52:54 -0400 | [diff] [blame] | 931 | return; |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 932 | if (surface == shell->panel) |
| 933 | return; |
| 934 | if (surface == shell->background) |
| 935 | return; |
Kristian Høgsberg | 10f097e | 2011-04-13 11:52:54 -0400 | [diff] [blame] | 936 | |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 937 | wlsc_surface_move(surface, (struct wlsc_input_device *) device, time); |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | static void |
| 941 | resize_binding(struct wl_input_device *device, uint32_t time, |
| 942 | uint32_t key, uint32_t button, uint32_t state, void *data) |
| 943 | { |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 944 | struct wl_shell *shell = data; |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 945 | struct wlsc_surface *surface = |
| 946 | (struct wlsc_surface *) device->pointer_focus; |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 947 | struct wl_resource *resource; |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 948 | uint32_t edges = 0; |
| 949 | int32_t x, y; |
| 950 | |
Kristian Høgsberg | a4a42f0 | 2011-09-08 16:56:57 -0400 | [diff] [blame] | 951 | if (surface == NULL || |
| 952 | surface->map_type == WLSC_SURFACE_MAP_FULLSCREEN) |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 953 | if (surface == shell->panel) |
| 954 | return; |
| 955 | if (surface == shell->background) |
| 956 | return; |
Kristian Høgsberg | 10f097e | 2011-04-13 11:52:54 -0400 | [diff] [blame] | 957 | |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 958 | x = device->grab_x - surface->x; |
| 959 | y = device->grab_y - surface->y; |
| 960 | |
| 961 | if (x < surface->width / 3) |
| 962 | edges |= WL_SHELL_RESIZE_LEFT; |
| 963 | else if (x < 2 * surface->width / 3) |
| 964 | edges |= 0; |
| 965 | else |
| 966 | edges |= WL_SHELL_RESIZE_RIGHT; |
| 967 | |
| 968 | if (y < surface->height / 3) |
| 969 | edges |= WL_SHELL_RESIZE_TOP; |
| 970 | else if (y < 2 * surface->height / 3) |
| 971 | edges |= 0; |
| 972 | else |
| 973 | edges |= WL_SHELL_RESIZE_BOTTOM; |
| 974 | |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 975 | resource = /* Find shell resource for surface client */ 0; |
| 976 | |
| 977 | /* ... or use wl_shell_surface */ |
| 978 | |
| 979 | wlsc_surface_resize(surface, (struct wlsc_input_device *) device, |
| 980 | time, edges, resource); |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | static void |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 984 | handle_hidden_surface_destroy(struct wl_listener *listener, |
| 985 | struct wl_resource *resource, uint32_t time) |
| 986 | { |
| 987 | struct hidden_surface *hidden = |
| 988 | container_of(listener, struct hidden_surface, destroy_listener); |
| 989 | |
| 990 | hidden_surface_destroy(hidden); |
| 991 | } |
| 992 | |
| 993 | static struct hidden_surface * |
| 994 | hidden_surface_create(struct wl_shell *shell, struct wlsc_surface *surface) |
| 995 | { |
| 996 | struct hidden_surface *hidden; |
| 997 | |
| 998 | hidden = malloc(sizeof *hidden); |
| 999 | if (!hidden) |
| 1000 | return NULL; |
| 1001 | |
| 1002 | hidden->surface = surface; |
| 1003 | hidden->shell = shell; |
| 1004 | hidden->destroy_listener.func = handle_hidden_surface_destroy; |
| 1005 | wl_list_insert(surface->surface.resource.destroy_listener_list.prev, |
| 1006 | &hidden->destroy_listener.link); |
| 1007 | |
| 1008 | return hidden; |
| 1009 | } |
| 1010 | |
| 1011 | static void |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 1012 | activate(struct wlsc_shell *base, struct wlsc_surface *es, |
| 1013 | struct wlsc_input_device *device, uint32_t time) |
| 1014 | { |
| 1015 | struct wl_shell *shell = container_of(base, struct wl_shell, shell); |
| 1016 | struct wlsc_compositor *compositor = shell->compositor; |
| 1017 | |
| 1018 | wlsc_surface_activate(es, device, time); |
| 1019 | |
Kristian Høgsberg | d6e5525 | 2011-10-11 23:41:17 -0400 | [diff] [blame] | 1020 | if (compositor->wxs) |
| 1021 | wlsc_xserver_surface_activate(es); |
| 1022 | |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 1023 | if (es == shell->background) { |
| 1024 | wl_list_remove(&es->link); |
| 1025 | wl_list_insert(compositor->surface_list.prev, &es->link); |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 1026 | } else if (shell->panel && !shell->locked) { |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 1027 | wl_list_remove(&shell->panel->link); |
| 1028 | wl_list_insert(&compositor->surface_list, &shell->panel->link); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | static void |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 1033 | lock(struct wlsc_shell *base) |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 1034 | { |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 1035 | struct wl_shell *shell = container_of(base, struct wl_shell, shell); |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 1036 | struct wl_list *surface_list = &shell->compositor->surface_list; |
| 1037 | struct wlsc_surface *cur; |
| 1038 | struct wlsc_surface *tmp; |
| 1039 | struct hidden_surface *hidden; |
| 1040 | struct wlsc_input_device *device; |
| 1041 | uint32_t time; |
| 1042 | |
| 1043 | if (shell->locked) |
| 1044 | return; |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 1045 | |
| 1046 | shell->locked = true; |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 1047 | |
| 1048 | /* Move all surfaces from compositor's list to our hidden list, |
| 1049 | * except the background. This way nothing else can show or |
| 1050 | * receive input events while we are locked. */ |
| 1051 | |
| 1052 | if (!wl_list_empty(&shell->hidden_surface_list)) { |
| 1053 | fprintf(stderr, |
| 1054 | "%s: Assertion failed: hidden_surface_list is not empty.\n", |
| 1055 | __func__); |
| 1056 | } |
| 1057 | |
| 1058 | wl_list_for_each_safe(cur, tmp, surface_list, link) { |
| 1059 | /* skip input device sprites, cur->surface is uninitialised */ |
| 1060 | if (cur->surface.resource.client == NULL) |
| 1061 | continue; |
| 1062 | |
| 1063 | if (cur == shell->background) |
| 1064 | continue; |
| 1065 | |
| 1066 | hidden = hidden_surface_create(shell, cur); |
| 1067 | if (!hidden) |
| 1068 | continue; |
| 1069 | |
| 1070 | wl_list_insert(shell->hidden_surface_list.prev, &hidden->link); |
| 1071 | wl_list_remove(&cur->link); |
| 1072 | wl_list_init(&cur->link); |
| 1073 | } |
| 1074 | |
| 1075 | /* reset pointer foci */ |
| 1076 | wlsc_compositor_repick(shell->compositor); |
| 1077 | |
| 1078 | /* reset keyboard foci */ |
| 1079 | time = wlsc_compositor_get_time(); |
| 1080 | wl_list_for_each(device, &shell->compositor->input_device_list, link) { |
| 1081 | wl_input_device_set_keyboard_focus(&device->input_device, |
| 1082 | NULL, time); |
| 1083 | } |
| 1084 | |
| 1085 | /* TODO: disable bindings that should not work while locked. */ |
| 1086 | |
| 1087 | /* All this must be undone in resume_desktop(). */ |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | static void |
| 1091 | unlock(struct wlsc_shell *base) |
| 1092 | { |
| 1093 | struct wl_shell *shell = container_of(base, struct wl_shell, shell); |
| 1094 | |
| 1095 | if (!shell->locked) { |
| 1096 | wlsc_compositor_wake(shell->compositor); |
| 1097 | return; |
| 1098 | } |
| 1099 | |
| 1100 | /* If desktop-shell client has gone away, unlock immediately. */ |
| 1101 | if (!shell->child.desktop_shell) { |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 1102 | resume_desktop(shell); |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 1103 | return; |
| 1104 | } |
| 1105 | |
| 1106 | if (shell->prepare_event_sent) |
| 1107 | return; |
| 1108 | |
| 1109 | wl_resource_post_event(shell->child.desktop_shell, |
| 1110 | DESKTOP_SHELL_PREPARE_LOCK_SURFACE); |
| 1111 | shell->prepare_event_sent = true; |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | static void |
Kristian Høgsberg | 32e24cc | 2011-11-09 12:07:35 -0500 | [diff] [blame] | 1115 | map(struct wlsc_shell *base, |
| 1116 | struct wlsc_surface *surface, int32_t width, int32_t height) |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 1117 | { |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 1118 | struct wl_shell *shell = container_of(base, struct wl_shell, shell); |
| 1119 | struct wlsc_compositor *compositor = shell->compositor; |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 1120 | struct hidden_surface *hidden; |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 1121 | |
Kristian Høgsberg | 32e24cc | 2011-11-09 12:07:35 -0500 | [diff] [blame] | 1122 | /* Map background at the bottom of the stack, panel on top, |
| 1123 | everything else just below panel. */ |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 1124 | if (surface == shell->background) { |
Kristian Høgsberg | 32e24cc | 2011-11-09 12:07:35 -0500 | [diff] [blame] | 1125 | wl_list_insert(compositor->surface_list.prev, &surface->link); |
Kristian Høgsberg | 32e24cc | 2011-11-09 12:07:35 -0500 | [diff] [blame] | 1126 | |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 1127 | } else if (shell->locked) { |
| 1128 | wl_list_init(&surface->link); |
| 1129 | |
| 1130 | hidden = hidden_surface_create(shell, surface); |
| 1131 | if (!hidden) |
| 1132 | goto out; |
| 1133 | |
| 1134 | /* panel positioning is fixed on resume */ |
| 1135 | wl_list_insert(&shell->hidden_surface_list, &hidden->link); |
| 1136 | } else { |
| 1137 | if (surface == shell->panel) |
| 1138 | wl_list_insert(&compositor->surface_list, |
| 1139 | &surface->link); |
| 1140 | else |
| 1141 | wl_list_insert(&shell->panel->link, &surface->link); |
| 1142 | } |
| 1143 | |
| 1144 | out: |
Kristian Høgsberg | 4677013 | 2011-11-09 12:38:53 -0500 | [diff] [blame] | 1145 | if (surface->map_type == WLSC_SURFACE_MAP_TOPLEVEL) { |
| 1146 | surface->x = 10 + random() % 400; |
| 1147 | surface->y = 10 + random() % 400; |
| 1148 | } |
| 1149 | |
Kristian Høgsberg | 32e24cc | 2011-11-09 12:07:35 -0500 | [diff] [blame] | 1150 | wlsc_surface_configure(surface, surface->x, surface->y, width, height); |
| 1151 | } |
| 1152 | |
| 1153 | static void |
| 1154 | configure(struct wlsc_shell *shell, struct wlsc_surface *surface, |
| 1155 | int32_t x, int32_t y, int32_t width, int32_t height) |
| 1156 | { |
| 1157 | struct wlsc_mode *current; |
| 1158 | |
| 1159 | if (surface->map_type == WLSC_SURFACE_MAP_FULLSCREEN) { |
| 1160 | current = surface->fullscreen_output->current; |
| 1161 | x = (current->width - surface->width) / 2; |
| 1162 | y = (current->height - surface->height) / 2; |
Kristian Høgsberg | 7a5c979 | 2011-06-18 06:12:54 -0400 | [diff] [blame] | 1163 | } |
Kristian Høgsberg | 32e24cc | 2011-11-09 12:07:35 -0500 | [diff] [blame] | 1164 | |
| 1165 | wlsc_surface_configure(surface, x, y, width, height); |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 1166 | } |
| 1167 | |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 1168 | static void |
Pekka Paalanen | 6cd281a | 2011-11-03 14:11:32 +0200 | [diff] [blame] | 1169 | desktop_shell_sigchld(struct wlsc_process *process, int status) |
| 1170 | { |
| 1171 | struct wl_shell *shell = |
| 1172 | container_of(process, struct wl_shell, child.process); |
| 1173 | |
| 1174 | shell->child.process.pid = 0; |
| 1175 | shell->child.client = NULL; /* already destroyed by wayland */ |
| 1176 | } |
| 1177 | |
| 1178 | static int |
| 1179 | launch_desktop_shell_process(struct wl_shell *shell) |
| 1180 | { |
Kristian Høgsberg | c4693c4 | 2011-11-14 14:57:17 -0500 | [diff] [blame] | 1181 | const char *shell_exe = LIBEXECDIR "/wayland-desktop-shell"; |
Pekka Paalanen | 6cd281a | 2011-11-03 14:11:32 +0200 | [diff] [blame] | 1182 | struct wlsc_compositor *compositor = shell->compositor; |
| 1183 | char s[32]; |
| 1184 | int sv[2], flags; |
| 1185 | |
| 1186 | if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) < 0) { |
| 1187 | fprintf(stderr, "socketpair failed\n"); |
| 1188 | return -1; |
| 1189 | } |
| 1190 | |
| 1191 | shell->child.process.pid = fork(); |
| 1192 | shell->child.process.cleanup = desktop_shell_sigchld; |
| 1193 | |
| 1194 | switch (shell->child.process.pid) { |
| 1195 | case 0: |
| 1196 | /* SOCK_CLOEXEC closes both ends, so we need to unset |
| 1197 | * the flag on the client fd. */ |
| 1198 | flags = fcntl(sv[1], F_GETFD); |
| 1199 | if (flags != -1) |
| 1200 | fcntl(sv[1], F_SETFD, flags & ~FD_CLOEXEC); |
| 1201 | |
| 1202 | snprintf(s, sizeof s, "%d", sv[1]); |
| 1203 | setenv("WAYLAND_SOCKET", s, 1); |
| 1204 | if (execl(shell_exe, shell_exe, NULL) < 0) |
| 1205 | fprintf(stderr, "%s: running '%s' failed: %m\n", |
| 1206 | __func__, shell_exe); |
| 1207 | exit(-1); |
| 1208 | |
| 1209 | default: |
| 1210 | close(sv[1]); |
| 1211 | shell->child.client = |
| 1212 | wl_client_create(compositor->wl_display, sv[0]); |
| 1213 | wlsc_watch_process(&shell->child.process); |
| 1214 | break; |
| 1215 | |
| 1216 | case -1: |
| 1217 | fprintf(stderr, "%s: fork failed: %m\n", __func__); |
| 1218 | return -1; |
| 1219 | } |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
| 1223 | static void |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 1224 | bind_shell(struct wl_client *client, void *data, uint32_t version, uint32_t id) |
| 1225 | { |
| 1226 | struct wl_shell *shell = data; |
| 1227 | |
| 1228 | wl_client_add_object(client, &wl_shell_interface, |
| 1229 | &shell_interface, id, shell); |
| 1230 | } |
| 1231 | |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 1232 | static void |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 1233 | unbind_desktop_shell(struct wl_resource *resource) |
| 1234 | { |
| 1235 | struct wl_shell *shell = resource->data; |
| 1236 | shell->child.desktop_shell = NULL; |
| 1237 | shell->prepare_event_sent = false; |
| 1238 | free(resource); |
| 1239 | } |
| 1240 | |
| 1241 | static void |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 1242 | bind_desktop_shell(struct wl_client *client, |
| 1243 | void *data, uint32_t version, uint32_t id) |
| 1244 | { |
| 1245 | struct wl_shell *shell = data; |
Pekka Paalanen | bbe6052 | 2011-11-03 14:11:33 +0200 | [diff] [blame] | 1246 | struct wl_resource *resource; |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 1247 | |
Pekka Paalanen | bbe6052 | 2011-11-03 14:11:33 +0200 | [diff] [blame] | 1248 | resource = wl_client_add_object(client, &desktop_shell_interface, |
| 1249 | &desktop_shell_implementation, |
| 1250 | id, shell); |
| 1251 | |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 1252 | if (client == shell->child.client) { |
| 1253 | resource->destroy = unbind_desktop_shell; |
| 1254 | shell->child.desktop_shell = resource; |
Pekka Paalanen | bbe6052 | 2011-11-03 14:11:33 +0200 | [diff] [blame] | 1255 | return; |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 1256 | } |
Pekka Paalanen | bbe6052 | 2011-11-03 14:11:33 +0200 | [diff] [blame] | 1257 | |
| 1258 | wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 1259 | "permission to bind desktop_shell denied"); |
| 1260 | wl_resource_destroy(resource, 0); |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 1261 | } |
| 1262 | |
Kristian Høgsberg | 6c709a3 | 2011-05-06 14:52:41 -0400 | [diff] [blame] | 1263 | int |
| 1264 | shell_init(struct wlsc_compositor *ec); |
| 1265 | |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1266 | WL_EXPORT int |
| 1267 | shell_init(struct wlsc_compositor *ec) |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 1268 | { |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 1269 | struct wl_shell *shell; |
| 1270 | |
| 1271 | shell = malloc(sizeof *shell); |
| 1272 | if (shell == NULL) |
| 1273 | return -1; |
| 1274 | |
Kristian Høgsberg | f0d9116 | 2011-10-11 22:44:23 -0400 | [diff] [blame] | 1275 | memset(shell, 0, sizeof *shell); |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 1276 | shell->compositor = ec; |
| 1277 | shell->shell.activate = activate; |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 1278 | shell->shell.lock = lock; |
Pekka Paalanen | 9ef3e01 | 2011-11-15 13:34:48 +0200 | [diff] [blame] | 1279 | shell->shell.unlock = unlock; |
Kristian Høgsberg | 32e24cc | 2011-11-09 12:07:35 -0500 | [diff] [blame] | 1280 | shell->shell.map = map; |
| 1281 | shell->shell.configure = configure; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1282 | shell->shell.set_selection_focus = wlsc_selection_set_focus; |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 1283 | |
Pekka Paalanen | f0fc70d | 2011-11-15 13:34:54 +0200 | [diff] [blame^] | 1284 | wl_list_init(&shell->hidden_surface_list); |
| 1285 | |
Kristian Høgsberg | 97d44aa | 2011-08-26 17:21:20 -0400 | [diff] [blame] | 1286 | if (wl_display_add_global(ec->wl_display, &wl_shell_interface, |
| 1287 | shell, bind_shell) == NULL) |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 1288 | return -1; |
| 1289 | |
Kristian Høgsberg | 7584062 | 2011-09-06 13:48:16 -0400 | [diff] [blame] | 1290 | if (wl_display_add_global(ec->wl_display, |
| 1291 | &desktop_shell_interface, |
| 1292 | shell, bind_desktop_shell) == NULL) |
| 1293 | return -1; |
| 1294 | |
Pekka Paalanen | 6cd281a | 2011-11-03 14:11:32 +0200 | [diff] [blame] | 1295 | if (launch_desktop_shell_process(shell) != 0) |
| 1296 | return -1; |
| 1297 | |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 1298 | wlsc_compositor_add_binding(ec, 0, BTN_LEFT, MODIFIER_SUPER, |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 1299 | move_binding, shell); |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 1300 | wlsc_compositor_add_binding(ec, 0, BTN_MIDDLE, MODIFIER_SUPER, |
Kristian Høgsberg | 02ec0a5 | 2011-04-23 13:04:11 -0400 | [diff] [blame] | 1301 | resize_binding, shell); |
| 1302 | |
| 1303 | ec->shell = &shell->shell; |
Kristian Høgsberg | 0793756 | 2011-04-12 17:25:42 -0400 | [diff] [blame] | 1304 | |
Kristian Høgsberg | 4cca349 | 2011-01-18 07:53:49 -0500 | [diff] [blame] | 1305 | return 0; |
| 1306 | } |