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