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