Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010-2012 Intel Corporation |
| 3 | * Copyright © 2011-2012 Collabora, Ltd. |
| 4 | * Copyright © 2013 Raspberry Pi Foundation |
| 5 | * Copyright © 2016 Quentin "Sardem FF7" Glidic |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | * copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation |
| 10 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | * and/or sell copies of the Software, and to permit persons to whom the |
| 12 | * Software is furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice (including the next |
| 15 | * paragraph) shall be included in all copies or substantial portions of the |
| 16 | * Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | * DEALINGS IN THE SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | |
| 29 | #include <stdbool.h> |
| 30 | |
| 31 | #include <wayland-server.h> |
| 32 | |
| 33 | #include "compositor.h" |
| 34 | #include "zalloc.h" |
Daniel Stone | 7dbb0e1 | 2016-11-24 15:30:41 +0000 | [diff] [blame] | 35 | #include "xdg-shell-unstable-v5-server-protocol.h" |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 36 | |
| 37 | #include "libweston-desktop.h" |
| 38 | #include "internal.h" |
| 39 | |
| 40 | #define WD_XDG_SHELL_PROTOCOL_VERSION 1 |
| 41 | |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 42 | struct weston_desktop_xdg_surface_state { |
| 43 | bool maximized; |
| 44 | bool fullscreen; |
| 45 | bool resizing; |
| 46 | bool activated; |
| 47 | }; |
| 48 | |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 49 | struct weston_desktop_xdg_surface_configure { |
| 50 | struct wl_list link; /* weston_desktop_xdg_surface::configure_list */ |
| 51 | uint32_t serial; |
| 52 | struct weston_desktop_xdg_surface_state state; |
| 53 | struct weston_size size; |
| 54 | }; |
| 55 | |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 56 | struct weston_desktop_xdg_surface { |
| 57 | struct wl_resource *resource; |
| 58 | struct weston_desktop_surface *surface; |
| 59 | struct weston_desktop *desktop; |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 60 | bool added; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 61 | struct wl_event_source *add_idle; |
| 62 | struct wl_event_source *configure_idle; |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 63 | struct wl_list configure_list; /* weston_desktop_xdg_surface_configure::link */ |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 64 | struct { |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 65 | struct weston_desktop_xdg_surface_state state; |
| 66 | struct weston_size size; |
| 67 | } pending; |
| 68 | struct { |
| 69 | struct weston_desktop_xdg_surface_state state; |
Quentin Glidic | ac394a1 | 2017-07-12 09:45:43 +0200 | [diff] [blame] | 70 | struct weston_size size; |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 71 | } next; |
| 72 | struct { |
| 73 | struct weston_desktop_xdg_surface_state state; |
| 74 | } current; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 75 | bool has_next_geometry; |
| 76 | struct weston_geometry next_geometry; |
| 77 | }; |
| 78 | |
| 79 | struct weston_desktop_xdg_popup { |
| 80 | struct wl_resource *resource; |
| 81 | struct weston_desktop_surface *popup; |
| 82 | struct weston_desktop *desktop; |
| 83 | struct weston_desktop_seat *seat; |
| 84 | struct wl_display *display; |
| 85 | }; |
| 86 | |
| 87 | static void |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 88 | weston_desktop_xdg_surface_ensure_added(struct weston_desktop_xdg_surface *surface) |
| 89 | { |
| 90 | if (surface->added) |
| 91 | return; |
| 92 | |
| 93 | if (surface->add_idle != NULL) |
| 94 | wl_event_source_remove(surface->add_idle); |
| 95 | surface->add_idle = NULL; |
| 96 | weston_desktop_api_surface_added(surface->desktop, surface->surface); |
| 97 | surface->added = true; |
| 98 | } |
| 99 | |
| 100 | static void |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 101 | weston_desktop_xdg_surface_send_configure(void *data) |
| 102 | { |
| 103 | struct weston_desktop_xdg_surface *surface = data; |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 104 | struct weston_desktop_xdg_surface_configure *configure; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 105 | uint32_t *s; |
| 106 | struct wl_array states; |
| 107 | |
| 108 | surface->configure_idle = NULL; |
| 109 | |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 110 | configure = zalloc(sizeof(struct weston_desktop_xdg_surface_configure)); |
| 111 | if (configure == NULL) { |
| 112 | struct weston_desktop_client *client = |
| 113 | weston_desktop_surface_get_client(surface->surface); |
| 114 | struct wl_client *wl_client = |
| 115 | weston_desktop_client_get_client(client); |
| 116 | wl_client_post_no_memory(wl_client); |
| 117 | return; |
| 118 | } |
| 119 | wl_list_insert(surface->configure_list.prev, &configure->link); |
| 120 | configure->serial = |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 121 | wl_display_next_serial(weston_desktop_get_display(surface->desktop)); |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 122 | configure->state = surface->pending.state; |
| 123 | configure->size = surface->pending.size; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 124 | |
| 125 | wl_array_init(&states); |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 126 | if (surface->pending.state.maximized) { |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 127 | s = wl_array_add(&states, sizeof(uint32_t)); |
| 128 | *s = XDG_SURFACE_STATE_MAXIMIZED; |
| 129 | } |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 130 | if (surface->pending.state.fullscreen) { |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 131 | s = wl_array_add(&states, sizeof(uint32_t)); |
| 132 | *s = XDG_SURFACE_STATE_FULLSCREEN; |
| 133 | } |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 134 | if (surface->pending.state.resizing) { |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 135 | s = wl_array_add(&states, sizeof(uint32_t)); |
| 136 | *s = XDG_SURFACE_STATE_RESIZING; |
| 137 | } |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 138 | if (surface->pending.state.activated) { |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 139 | s = wl_array_add(&states, sizeof(uint32_t)); |
| 140 | *s = XDG_SURFACE_STATE_ACTIVATED; |
| 141 | } |
| 142 | |
| 143 | xdg_surface_send_configure(surface->resource, |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 144 | surface->pending.size.width, |
| 145 | surface->pending.size.height, |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 146 | &states, |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 147 | configure->serial); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 148 | |
| 149 | wl_array_release(&states); |
| 150 | }; |
| 151 | |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 152 | static bool |
| 153 | weston_desktop_xdg_surface_state_compare(struct weston_desktop_xdg_surface *surface) |
| 154 | { |
| 155 | struct weston_surface *wsurface = |
| 156 | weston_desktop_surface_get_surface(surface->surface); |
| 157 | |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 158 | if (surface->pending.state.activated != surface->current.state.activated) |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 159 | return false; |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 160 | if (surface->pending.state.fullscreen != surface->current.state.fullscreen) |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 161 | return false; |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 162 | if (surface->pending.state.maximized != surface->current.state.maximized) |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 163 | return false; |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 164 | if (surface->pending.state.resizing != surface->current.state.resizing) |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 165 | return false; |
| 166 | |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 167 | if (wsurface->width == surface->pending.size.width && |
| 168 | wsurface->height == surface->pending.size.height) |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 169 | return true; |
| 170 | |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 171 | if (surface->pending.size.width == 0 && |
| 172 | surface->pending.size.height == 0) |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 173 | return true; |
| 174 | |
| 175 | return false; |
| 176 | } |
| 177 | |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 178 | static void |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 179 | weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface, |
| 180 | bool force) |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 181 | { |
| 182 | struct wl_display *display = weston_desktop_get_display(surface->desktop); |
| 183 | struct wl_event_loop *loop = wl_display_get_event_loop(display); |
Quentin Glidic | 218126d | 2017-07-11 13:31:36 +0200 | [diff] [blame] | 184 | bool pending_same = |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 185 | !force && weston_desktop_xdg_surface_state_compare(surface); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 186 | |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 187 | if (surface->configure_idle != NULL) { |
Quentin Glidic | 218126d | 2017-07-11 13:31:36 +0200 | [diff] [blame] | 188 | if (!pending_same) |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 189 | return; |
| 190 | |
| 191 | wl_event_source_remove(surface->configure_idle); |
| 192 | surface->configure_idle = NULL; |
| 193 | } else { |
Quentin Glidic | 218126d | 2017-07-11 13:31:36 +0200 | [diff] [blame] | 194 | if (pending_same) |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 195 | return; |
| 196 | |
| 197 | surface->configure_idle = |
| 198 | wl_event_loop_add_idle(loop, |
| 199 | weston_desktop_xdg_surface_send_configure, |
| 200 | surface); |
| 201 | } |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static void |
| 205 | weston_desktop_xdg_surface_set_maximized(struct weston_desktop_surface *dsurface, |
| 206 | void *user_data, bool maximized) |
| 207 | { |
| 208 | struct weston_desktop_xdg_surface *surface = user_data; |
| 209 | |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 210 | surface->pending.state.maximized = maximized; |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 211 | weston_desktop_xdg_surface_schedule_configure(surface, false); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void |
| 215 | weston_desktop_xdg_surface_set_fullscreen(struct weston_desktop_surface *dsurface, |
| 216 | void *user_data, bool fullscreen) |
| 217 | { |
| 218 | struct weston_desktop_xdg_surface *surface = user_data; |
| 219 | |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 220 | surface->pending.state.fullscreen = fullscreen; |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 221 | weston_desktop_xdg_surface_schedule_configure(surface, false); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static void |
| 225 | weston_desktop_xdg_surface_set_resizing(struct weston_desktop_surface *dsurface, |
| 226 | void *user_data, bool resizing) |
| 227 | { |
| 228 | struct weston_desktop_xdg_surface *surface = user_data; |
| 229 | |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 230 | surface->pending.state.resizing = resizing; |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 231 | weston_desktop_xdg_surface_schedule_configure(surface, false); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static void |
| 235 | weston_desktop_xdg_surface_set_activated(struct weston_desktop_surface *dsurface, |
| 236 | void *user_data, bool activated) |
| 237 | { |
| 238 | struct weston_desktop_xdg_surface *surface = user_data; |
| 239 | |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 240 | surface->pending.state.activated = activated; |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 241 | weston_desktop_xdg_surface_schedule_configure(surface, false); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static void |
| 245 | weston_desktop_xdg_surface_set_size(struct weston_desktop_surface *dsurface, |
| 246 | void *user_data, |
| 247 | int32_t width, int32_t height) |
| 248 | { |
| 249 | struct weston_desktop_xdg_surface *surface = user_data; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 250 | |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 251 | surface->pending.size.width = width; |
| 252 | surface->pending.size.height = height; |
Quentin Glidic | a56b053 | 2016-09-13 10:05:58 +0200 | [diff] [blame] | 253 | |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 254 | weston_desktop_xdg_surface_schedule_configure(surface, false); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static void |
| 258 | weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface, |
Quentin Glidic | 003da88 | 2016-08-15 12:21:39 +0200 | [diff] [blame] | 259 | void *user_data, |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 260 | int32_t sx, int32_t sy) |
| 261 | { |
| 262 | struct weston_desktop_xdg_surface *surface = user_data; |
| 263 | struct weston_surface *wsurface = |
| 264 | weston_desktop_surface_get_surface(surface->surface); |
| 265 | bool reconfigure = false; |
| 266 | |
Philipp Kerling | c623902 | 2017-07-26 14:02:21 +0200 | [diff] [blame] | 267 | /* TODO: use the window geometry and not the surface size here |
| 268 | * We need to check the next geometry if there is one, but not accept it |
| 269 | * until we checked it, maybe. |
| 270 | */ |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 271 | if (surface->next.state.maximized || surface->next.state.fullscreen) |
Quentin Glidic | ac394a1 | 2017-07-12 09:45:43 +0200 | [diff] [blame] | 272 | reconfigure = surface->next.size.width != wsurface->width || |
| 273 | surface->next.size.height != wsurface->height; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 274 | |
| 275 | if (reconfigure) { |
Quentin Glidic | d51f826 | 2017-04-13 20:25:27 +0200 | [diff] [blame] | 276 | weston_desktop_xdg_surface_schedule_configure(surface, true); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 277 | } else { |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 278 | surface->current.state = surface->next.state; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 279 | if (surface->has_next_geometry) { |
| 280 | surface->has_next_geometry = false; |
| 281 | weston_desktop_surface_set_geometry(surface->surface, |
| 282 | surface->next_geometry); |
| 283 | } |
| 284 | |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 285 | weston_desktop_xdg_surface_ensure_added(surface); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 286 | weston_desktop_api_committed(surface->desktop, surface->surface, |
| 287 | sx, sy); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static void |
| 292 | weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface, |
| 293 | uint32_t serial, void *user_data) |
| 294 | { |
| 295 | struct weston_desktop_client *client = |
| 296 | weston_desktop_surface_get_client(dsurface); |
| 297 | |
| 298 | xdg_shell_send_ping(weston_desktop_client_get_resource(client), |
| 299 | serial); |
| 300 | } |
| 301 | |
| 302 | static void |
| 303 | weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface, |
| 304 | void *user_data) |
| 305 | { |
| 306 | struct weston_desktop_xdg_surface *surface = user_data; |
| 307 | |
| 308 | xdg_surface_send_close(surface->resource); |
| 309 | } |
| 310 | |
| 311 | static bool |
| 312 | weston_desktop_xdg_surface_get_maximized(struct weston_desktop_surface *dsurface, |
| 313 | void *user_data) |
| 314 | { |
| 315 | struct weston_desktop_xdg_surface *surface = user_data; |
| 316 | |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 317 | return surface->current.state.maximized; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static bool |
| 321 | weston_desktop_xdg_surface_get_fullscreen(struct weston_desktop_surface *dsurface, |
| 322 | void *user_data) |
| 323 | { |
| 324 | struct weston_desktop_xdg_surface *surface = user_data; |
| 325 | |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 326 | return surface->current.state.fullscreen; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static bool |
| 330 | weston_desktop_xdg_surface_get_resizing(struct weston_desktop_surface *dsurface, |
| 331 | void *user_data) |
| 332 | { |
| 333 | struct weston_desktop_xdg_surface *surface = user_data; |
| 334 | |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 335 | return surface->current.state.resizing; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | static bool |
| 339 | weston_desktop_xdg_surface_get_activated(struct weston_desktop_surface *dsurface, |
| 340 | void *user_data) |
| 341 | { |
| 342 | struct weston_desktop_xdg_surface *surface = user_data; |
| 343 | |
Quentin Glidic | 19d1f6e | 2017-07-12 09:42:57 +0200 | [diff] [blame] | 344 | return surface->current.state.activated; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static void |
| 348 | weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface, |
| 349 | void *user_data) |
| 350 | { |
| 351 | struct weston_desktop_xdg_surface *surface = user_data; |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 352 | struct weston_desktop_xdg_surface_configure *configure, *temp; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 353 | |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 354 | if (surface->added) |
| 355 | weston_desktop_api_surface_removed(surface->desktop, |
| 356 | surface->surface); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 357 | |
| 358 | if (surface->add_idle != NULL) |
| 359 | wl_event_source_remove(surface->add_idle); |
| 360 | |
| 361 | if (surface->configure_idle != NULL) |
| 362 | wl_event_source_remove(surface->configure_idle); |
| 363 | |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 364 | wl_list_for_each_safe(configure, temp, &surface->configure_list, link) |
| 365 | free(configure); |
| 366 | |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 367 | free(surface); |
| 368 | } |
| 369 | |
| 370 | static void |
| 371 | weston_desktop_xdg_surface_protocol_set_parent(struct wl_client *wl_client, |
| 372 | struct wl_resource *resource, |
| 373 | struct wl_resource *parent_resource) |
| 374 | { |
| 375 | struct weston_desktop_surface *dsurface = |
| 376 | wl_resource_get_user_data(resource); |
| 377 | struct weston_desktop_xdg_surface *surface = |
| 378 | weston_desktop_surface_get_implementation_data(dsurface); |
| 379 | struct weston_desktop_surface *parent = NULL; |
| 380 | |
| 381 | if (parent_resource != NULL) |
| 382 | parent = wl_resource_get_user_data(parent_resource); |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 383 | |
| 384 | weston_desktop_xdg_surface_ensure_added(surface); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 385 | weston_desktop_api_set_parent(surface->desktop, dsurface, parent); |
| 386 | } |
| 387 | |
| 388 | static void |
| 389 | weston_desktop_xdg_surface_protocol_set_title(struct wl_client *wl_client, |
| 390 | struct wl_resource *resource, |
| 391 | const char *title) |
| 392 | { |
| 393 | struct weston_desktop_surface *surface = |
| 394 | wl_resource_get_user_data(resource); |
| 395 | |
| 396 | weston_desktop_surface_set_title(surface, title); |
| 397 | } |
| 398 | |
| 399 | static void |
| 400 | weston_desktop_xdg_surface_protocol_set_app_id(struct wl_client *wl_client, |
| 401 | struct wl_resource *resource, |
| 402 | const char *app_id) |
| 403 | { |
| 404 | struct weston_desktop_surface *surface = |
| 405 | wl_resource_get_user_data(resource); |
| 406 | |
| 407 | weston_desktop_surface_set_app_id(surface, app_id); |
| 408 | } |
| 409 | |
| 410 | static void |
| 411 | weston_desktop_xdg_surface_protocol_show_window_menu(struct wl_client *wl_client, |
| 412 | struct wl_resource *resource, |
| 413 | struct wl_resource *seat_resource, |
| 414 | uint32_t serial, |
| 415 | int32_t x, int32_t y) |
| 416 | { |
| 417 | struct weston_desktop_surface *dsurface = |
| 418 | wl_resource_get_user_data(resource); |
| 419 | struct weston_seat *seat = |
| 420 | wl_resource_get_user_data(seat_resource); |
| 421 | struct weston_desktop_xdg_surface *surface = |
| 422 | weston_desktop_surface_get_implementation_data(dsurface); |
| 423 | |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 424 | weston_desktop_xdg_surface_ensure_added(surface); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 425 | weston_desktop_api_show_window_menu(surface->desktop, dsurface, seat, x, y); |
| 426 | } |
| 427 | |
| 428 | static void |
| 429 | weston_desktop_xdg_surface_protocol_move(struct wl_client *wl_client, |
| 430 | struct wl_resource *resource, |
| 431 | struct wl_resource *seat_resource, |
| 432 | uint32_t serial) |
| 433 | { |
| 434 | struct weston_desktop_surface *dsurface = |
| 435 | wl_resource_get_user_data(resource); |
| 436 | struct weston_seat *seat = |
| 437 | wl_resource_get_user_data(seat_resource); |
| 438 | struct weston_desktop_xdg_surface *surface = |
| 439 | weston_desktop_surface_get_implementation_data(dsurface); |
| 440 | |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 441 | weston_desktop_xdg_surface_ensure_added(surface); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 442 | weston_desktop_api_move(surface->desktop, dsurface, seat, serial); |
| 443 | } |
| 444 | |
| 445 | static void |
| 446 | weston_desktop_xdg_surface_protocol_resize(struct wl_client *wl_client, |
| 447 | struct wl_resource *resource, |
| 448 | struct wl_resource *seat_resource, |
| 449 | uint32_t serial, |
| 450 | enum xdg_surface_resize_edge edges) |
| 451 | { |
| 452 | struct weston_desktop_surface *dsurface = |
| 453 | wl_resource_get_user_data(resource); |
| 454 | struct weston_seat *seat = |
| 455 | wl_resource_get_user_data(seat_resource); |
| 456 | struct weston_desktop_xdg_surface *surface = |
| 457 | weston_desktop_surface_get_implementation_data(dsurface); |
Armin Krezović | 4e2fa0a | 2016-09-10 19:11:21 +0200 | [diff] [blame] | 458 | enum weston_desktop_surface_edge surf_edges = |
| 459 | (enum weston_desktop_surface_edge) edges; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 460 | |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 461 | weston_desktop_xdg_surface_ensure_added(surface); |
Armin Krezović | 4e2fa0a | 2016-09-10 19:11:21 +0200 | [diff] [blame] | 462 | weston_desktop_api_resize(surface->desktop, dsurface, seat, serial, surf_edges); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | static void |
| 466 | weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client, |
| 467 | struct wl_resource *resource, |
| 468 | uint32_t serial) |
| 469 | { |
| 470 | struct weston_desktop_surface *dsurface = |
| 471 | wl_resource_get_user_data(resource); |
| 472 | struct weston_desktop_xdg_surface *surface = |
| 473 | weston_desktop_surface_get_implementation_data(dsurface); |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 474 | struct weston_desktop_xdg_surface_configure *configure, *temp; |
| 475 | bool found = false; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 476 | |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 477 | wl_list_for_each_safe(configure, temp, &surface->configure_list, link) { |
| 478 | if (configure->serial < serial) { |
| 479 | wl_list_remove(&configure->link); |
| 480 | free(configure); |
| 481 | } else if (configure->serial == serial) { |
| 482 | wl_list_remove(&configure->link); |
| 483 | found = true; |
Derek Foreman | e371552 | 2017-07-26 14:35:58 -0500 | [diff] [blame] | 484 | break; |
| 485 | } else { |
| 486 | break; |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 487 | } |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 488 | } |
| 489 | if (!found) { |
| 490 | struct weston_desktop_client *client = |
| 491 | weston_desktop_surface_get_client(dsurface); |
| 492 | struct wl_resource *client_resource = |
| 493 | weston_desktop_client_get_resource(client); |
| 494 | wl_resource_post_error(client_resource, |
| 495 | XDG_SHELL_ERROR_DEFUNCT_SURFACES, |
| 496 | "Wrong configure serial: %u", serial); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 497 | return; |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 498 | } |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 499 | |
Quentin Glidic | 749637a | 2017-07-18 12:59:14 +0200 | [diff] [blame] | 500 | surface->next.state = configure->state; |
| 501 | surface->next.size = configure->size; |
| 502 | |
| 503 | free(configure); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static void |
| 507 | weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client, |
| 508 | struct wl_resource *resource, |
| 509 | int32_t x, int32_t y, |
| 510 | int32_t width, int32_t height) |
| 511 | { |
| 512 | struct weston_desktop_surface *dsurface = |
| 513 | wl_resource_get_user_data(resource); |
| 514 | struct weston_desktop_xdg_surface *surface = |
| 515 | weston_desktop_surface_get_implementation_data(dsurface); |
| 516 | |
| 517 | surface->has_next_geometry = true; |
| 518 | surface->next_geometry.x = x; |
| 519 | surface->next_geometry.y = y; |
| 520 | surface->next_geometry.width = width; |
| 521 | surface->next_geometry.height = height; |
| 522 | } |
| 523 | |
| 524 | static void |
| 525 | weston_desktop_xdg_surface_protocol_set_maximized(struct wl_client *wl_client, |
| 526 | struct wl_resource *resource) |
| 527 | { |
| 528 | struct weston_desktop_surface *dsurface = |
| 529 | wl_resource_get_user_data(resource); |
| 530 | struct weston_desktop_xdg_surface *surface = |
| 531 | weston_desktop_surface_get_implementation_data(dsurface); |
| 532 | |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 533 | weston_desktop_xdg_surface_ensure_added(surface); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 534 | weston_desktop_api_maximized_requested(surface->desktop, dsurface, true); |
| 535 | } |
| 536 | |
| 537 | static void |
| 538 | weston_desktop_xdg_surface_protocol_unset_maximized(struct wl_client *wl_client, |
| 539 | struct wl_resource *resource) |
| 540 | { |
| 541 | struct weston_desktop_surface *dsurface = |
| 542 | wl_resource_get_user_data(resource); |
| 543 | struct weston_desktop_xdg_surface *surface = |
| 544 | weston_desktop_surface_get_implementation_data(dsurface); |
| 545 | |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 546 | weston_desktop_xdg_surface_ensure_added(surface); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 547 | weston_desktop_api_maximized_requested(surface->desktop, dsurface, false); |
| 548 | } |
| 549 | |
| 550 | static void |
| 551 | weston_desktop_xdg_surface_protocol_set_fullscreen(struct wl_client *wl_client, |
| 552 | struct wl_resource *resource, |
| 553 | struct wl_resource *output_resource) |
| 554 | { |
| 555 | struct weston_desktop_surface *dsurface = |
| 556 | wl_resource_get_user_data(resource); |
| 557 | struct weston_desktop_xdg_surface *surface = |
| 558 | weston_desktop_surface_get_implementation_data(dsurface); |
| 559 | struct weston_output *output = NULL; |
| 560 | |
| 561 | if (output_resource != NULL) |
Pekka Paalanen | 9ffb250 | 2017-03-27 15:14:32 +0300 | [diff] [blame] | 562 | output = weston_output_from_resource(output_resource); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 563 | |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 564 | weston_desktop_xdg_surface_ensure_added(surface); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 565 | weston_desktop_api_fullscreen_requested(surface->desktop, dsurface, |
| 566 | true, output); |
| 567 | } |
| 568 | |
| 569 | static void |
| 570 | weston_desktop_xdg_surface_protocol_unset_fullscreen(struct wl_client *wl_client, |
| 571 | struct wl_resource *resource) |
| 572 | { |
| 573 | struct weston_desktop_surface *dsurface = |
| 574 | wl_resource_get_user_data(resource); |
| 575 | struct weston_desktop_xdg_surface *surface = |
| 576 | weston_desktop_surface_get_implementation_data(dsurface); |
| 577 | |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 578 | weston_desktop_xdg_surface_ensure_added(surface); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 579 | weston_desktop_api_fullscreen_requested(surface->desktop, dsurface, |
| 580 | false, NULL); |
| 581 | } |
| 582 | |
| 583 | static void |
| 584 | weston_desktop_xdg_surface_protocol_set_minimized(struct wl_client *wl_client, |
| 585 | struct wl_resource *resource) |
| 586 | { |
| 587 | struct weston_desktop_surface *dsurface = |
| 588 | wl_resource_get_user_data(resource); |
| 589 | struct weston_desktop_xdg_surface *surface = |
| 590 | weston_desktop_surface_get_implementation_data(dsurface); |
| 591 | |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 592 | weston_desktop_xdg_surface_ensure_added(surface); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 593 | weston_desktop_api_minimized_requested(surface->desktop, dsurface); |
| 594 | } |
| 595 | |
| 596 | static const struct xdg_surface_interface weston_desktop_xdg_surface_implementation = { |
| 597 | .destroy = weston_desktop_destroy_request, |
| 598 | .set_parent = weston_desktop_xdg_surface_protocol_set_parent, |
| 599 | .set_title = weston_desktop_xdg_surface_protocol_set_title, |
| 600 | .set_app_id = weston_desktop_xdg_surface_protocol_set_app_id, |
| 601 | .show_window_menu = weston_desktop_xdg_surface_protocol_show_window_menu, |
| 602 | .move = weston_desktop_xdg_surface_protocol_move, |
| 603 | .resize = weston_desktop_xdg_surface_protocol_resize, |
| 604 | .ack_configure = weston_desktop_xdg_surface_protocol_ack_configure, |
| 605 | .set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry, |
| 606 | .set_maximized = weston_desktop_xdg_surface_protocol_set_maximized, |
| 607 | .unset_maximized = weston_desktop_xdg_surface_protocol_unset_maximized, |
| 608 | .set_fullscreen = weston_desktop_xdg_surface_protocol_set_fullscreen, |
| 609 | .unset_fullscreen = weston_desktop_xdg_surface_protocol_unset_fullscreen, |
| 610 | .set_minimized = weston_desktop_xdg_surface_protocol_set_minimized, |
| 611 | }; |
| 612 | |
| 613 | static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = { |
| 614 | .set_maximized = weston_desktop_xdg_surface_set_maximized, |
| 615 | .set_fullscreen = weston_desktop_xdg_surface_set_fullscreen, |
| 616 | .set_resizing = weston_desktop_xdg_surface_set_resizing, |
| 617 | .set_activated = weston_desktop_xdg_surface_set_activated, |
| 618 | .set_size = weston_desktop_xdg_surface_set_size, |
| 619 | .committed = weston_desktop_xdg_surface_committed, |
| 620 | .ping = weston_desktop_xdg_surface_ping, |
| 621 | .close = weston_desktop_xdg_surface_close, |
| 622 | |
| 623 | .get_maximized = weston_desktop_xdg_surface_get_maximized, |
| 624 | .get_fullscreen = weston_desktop_xdg_surface_get_fullscreen, |
| 625 | .get_resizing = weston_desktop_xdg_surface_get_resizing, |
| 626 | .get_activated = weston_desktop_xdg_surface_get_activated, |
| 627 | |
| 628 | .destroy = weston_desktop_xdg_surface_destroy, |
| 629 | }; |
| 630 | |
| 631 | static void |
| 632 | weston_desktop_xdg_popup_close(struct weston_desktop_surface *dsurface, |
| 633 | void *user_data) |
| 634 | { |
| 635 | struct weston_desktop_xdg_popup *popup = user_data; |
| 636 | |
| 637 | xdg_popup_send_popup_done(popup->resource); |
| 638 | } |
| 639 | |
| 640 | static void |
| 641 | weston_desktop_xdg_popup_destroy(struct weston_desktop_surface *dsurface, |
| 642 | void *user_data) |
| 643 | { |
| 644 | struct weston_desktop_xdg_popup *popup = user_data; |
| 645 | struct weston_desktop_surface *topmost; |
| 646 | struct weston_desktop_client *client = |
| 647 | weston_desktop_surface_get_client(popup->popup); |
| 648 | |
| 649 | if (!weston_desktop_surface_get_grab(popup->popup)) |
| 650 | goto end; |
| 651 | |
| 652 | topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat); |
| 653 | if (topmost != popup->popup) { |
| 654 | struct wl_resource *client_resource = |
| 655 | weston_desktop_client_get_resource(client); |
| 656 | |
| 657 | wl_resource_post_error(client_resource, |
| 658 | XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP, |
| 659 | "xdg_popup was destroyed while it was not the topmost popup."); |
| 660 | } |
| 661 | |
| 662 | weston_desktop_surface_popup_ungrab(popup->popup, popup->seat); |
| 663 | |
| 664 | end: |
| 665 | free(popup); |
| 666 | } |
| 667 | |
| 668 | static const struct xdg_popup_interface weston_desktop_xdg_popup_implementation = { |
| 669 | .destroy = weston_desktop_destroy_request, |
| 670 | }; |
| 671 | |
| 672 | static const struct weston_desktop_surface_implementation weston_desktop_xdg_popup_internal_implementation = { |
| 673 | .close = weston_desktop_xdg_popup_close, |
| 674 | |
| 675 | .destroy = weston_desktop_xdg_popup_destroy, |
| 676 | }; |
| 677 | |
| 678 | static void |
| 679 | weston_desktop_xdg_shell_protocol_use_unstable_version(struct wl_client *wl_client, |
| 680 | struct wl_resource *resource, |
| 681 | int32_t version) |
| 682 | { |
| 683 | if (version > 1) { |
| 684 | wl_resource_post_error(resource, |
| 685 | 1, "xdg_shell version not supported"); |
| 686 | return; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | static void |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 691 | weston_desktop_xdg_surface_add_idle_callback(void *user_data) |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 692 | { |
| 693 | struct weston_desktop_xdg_surface *surface = user_data; |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 694 | |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 695 | surface->add_idle = NULL; |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 696 | weston_desktop_xdg_surface_ensure_added(surface); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | static void |
| 700 | weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client, |
| 701 | struct wl_resource *resource, |
| 702 | uint32_t id, |
| 703 | struct wl_resource *surface_resource) |
| 704 | { |
| 705 | struct weston_desktop_client *client = |
| 706 | wl_resource_get_user_data(resource); |
| 707 | struct weston_desktop *desktop = |
| 708 | weston_desktop_client_get_desktop(client); |
| 709 | struct weston_surface *wsurface = |
| 710 | wl_resource_get_user_data(surface_resource); |
| 711 | struct weston_desktop_xdg_surface *surface; |
| 712 | struct wl_display *display = weston_desktop_get_display(desktop); |
| 713 | struct wl_event_loop *loop = wl_display_get_event_loop(display); |
| 714 | |
| 715 | if (weston_surface_set_role(wsurface, "xdg_surface", resource, XDG_SHELL_ERROR_ROLE) < 0) |
| 716 | return; |
| 717 | |
| 718 | surface = zalloc(sizeof(struct weston_desktop_xdg_surface)); |
| 719 | if (surface == NULL) { |
| 720 | wl_client_post_no_memory(wl_client); |
| 721 | return; |
| 722 | } |
| 723 | |
| 724 | surface->desktop = desktop; |
| 725 | |
| 726 | surface->surface = |
| 727 | weston_desktop_surface_create(surface->desktop, client, |
| 728 | wsurface, |
| 729 | &weston_desktop_xdg_surface_internal_implementation, |
| 730 | surface); |
| 731 | if (surface->surface == NULL) { |
| 732 | free(surface); |
| 733 | return; |
| 734 | } |
| 735 | |
| 736 | surface->resource = |
| 737 | weston_desktop_surface_add_resource(surface->surface, |
| 738 | &xdg_surface_interface, |
| 739 | &weston_desktop_xdg_surface_implementation, |
| 740 | id, NULL); |
| 741 | if (surface->resource == NULL) |
| 742 | return; |
| 743 | |
| 744 | surface->add_idle = |
| 745 | wl_event_loop_add_idle(loop, |
Quentin Glidic | 6967f0e | 2016-08-18 15:51:38 +0200 | [diff] [blame] | 746 | weston_desktop_xdg_surface_add_idle_callback, |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 747 | surface); |
Arnaud Vrac | 3000a1c | 2017-08-05 13:59:01 +0200 | [diff] [blame] | 748 | |
| 749 | wl_list_init(&surface->configure_list); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | static void |
| 753 | weston_desktop_xdg_shell_protocol_get_xdg_popup(struct wl_client *wl_client, |
| 754 | struct wl_resource *resource, |
| 755 | uint32_t id, |
| 756 | struct wl_resource *surface_resource, |
| 757 | struct wl_resource *parent_resource, |
| 758 | struct wl_resource *seat_resource, |
| 759 | uint32_t serial, |
| 760 | int32_t x, int32_t y) |
| 761 | { |
| 762 | struct weston_desktop_client *client = |
| 763 | wl_resource_get_user_data(resource); |
| 764 | struct weston_surface *wsurface = |
| 765 | wl_resource_get_user_data(surface_resource); |
| 766 | struct weston_surface *wparent = |
| 767 | wl_resource_get_user_data(parent_resource); |
| 768 | struct weston_seat *wseat = wl_resource_get_user_data(seat_resource); |
| 769 | struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat); |
| 770 | struct weston_desktop_surface *parent, *topmost; |
| 771 | bool parent_is_popup, parent_is_xdg; |
| 772 | struct weston_desktop_xdg_popup *popup; |
| 773 | |
| 774 | if (weston_surface_set_role(wsurface, "xdg_popup", resource, XDG_SHELL_ERROR_ROLE) < 0) |
| 775 | return; |
| 776 | |
| 777 | if (!weston_surface_is_desktop_surface(wparent)) { |
| 778 | wl_resource_post_error(resource, |
| 779 | XDG_SHELL_ERROR_INVALID_POPUP_PARENT, |
| 780 | "xdg_popup parent was invalid"); |
| 781 | return; |
| 782 | } |
| 783 | |
| 784 | parent = weston_surface_get_desktop_surface(wparent); |
| 785 | parent_is_xdg = |
| 786 | weston_desktop_surface_has_implementation(parent, |
| 787 | &weston_desktop_xdg_surface_internal_implementation); |
| 788 | parent_is_popup = |
| 789 | weston_desktop_surface_has_implementation(parent, |
| 790 | &weston_desktop_xdg_popup_internal_implementation); |
| 791 | |
| 792 | if (!parent_is_xdg && !parent_is_popup) { |
| 793 | wl_resource_post_error(resource, |
| 794 | XDG_SHELL_ERROR_INVALID_POPUP_PARENT, |
| 795 | "xdg_popup parent was invalid"); |
| 796 | return; |
| 797 | } |
| 798 | |
| 799 | topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat); |
| 800 | if ((topmost == NULL && parent_is_popup) || |
| 801 | (topmost != NULL && topmost != parent)) { |
| 802 | wl_resource_post_error(resource, |
| 803 | XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP, |
| 804 | "xdg_popup was not created on the topmost popup"); |
| 805 | return; |
| 806 | } |
| 807 | |
| 808 | popup = zalloc(sizeof(struct weston_desktop_xdg_popup)); |
| 809 | if (popup == NULL) { |
| 810 | wl_client_post_no_memory(wl_client); |
| 811 | return; |
| 812 | } |
| 813 | |
| 814 | popup->desktop = weston_desktop_client_get_desktop(client); |
| 815 | popup->display = weston_desktop_get_display(popup->desktop); |
| 816 | popup->seat = seat; |
| 817 | |
| 818 | popup->popup = |
| 819 | weston_desktop_surface_create(popup->desktop, client, wsurface, |
| 820 | &weston_desktop_xdg_popup_internal_implementation, |
| 821 | popup); |
| 822 | if (popup->popup == NULL) { |
| 823 | free(popup); |
| 824 | return; |
| 825 | } |
| 826 | |
| 827 | popup->resource = |
| 828 | weston_desktop_surface_add_resource(popup->popup, |
| 829 | &xdg_popup_interface, |
| 830 | &weston_desktop_xdg_popup_implementation, |
| 831 | id, NULL); |
| 832 | if (popup->resource == NULL) |
| 833 | return; |
| 834 | |
| 835 | weston_desktop_surface_set_relative_to(popup->popup, parent, x, y, false); |
| 836 | weston_desktop_surface_popup_grab(popup->popup, popup->seat, serial); |
| 837 | } |
| 838 | |
| 839 | static void |
| 840 | weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client, |
| 841 | struct wl_resource *resource, |
| 842 | uint32_t serial) |
| 843 | { |
| 844 | struct weston_desktop_client *client = |
| 845 | wl_resource_get_user_data(resource); |
| 846 | |
| 847 | weston_desktop_client_pong(client, serial); |
| 848 | } |
| 849 | |
| 850 | static const struct xdg_shell_interface weston_desktop_xdg_shell_implementation = { |
| 851 | .destroy = weston_desktop_destroy_request, |
| 852 | .use_unstable_version = weston_desktop_xdg_shell_protocol_use_unstable_version, |
| 853 | .get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface, |
| 854 | .get_xdg_popup = weston_desktop_xdg_shell_protocol_get_xdg_popup, |
| 855 | .pong = weston_desktop_xdg_shell_protocol_pong, |
| 856 | }; |
| 857 | |
| 858 | static int |
| 859 | xdg_shell_unversioned_dispatch(const void *implementation, |
| 860 | void *_target, uint32_t opcode, |
| 861 | const struct wl_message *message, |
| 862 | union wl_argument *args) |
| 863 | { |
| 864 | struct wl_resource *resource = _target; |
| 865 | struct weston_desktop_client *client = |
| 866 | wl_resource_get_user_data(resource); |
| 867 | |
| 868 | if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) { |
| 869 | wl_resource_post_error(resource, |
| 870 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 871 | "must call use_unstable_version first"); |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | #define XDG_SERVER_VERSION 5 |
| 876 | |
| 877 | if (args[0].i != XDG_SERVER_VERSION) { |
| 878 | wl_resource_post_error(resource, |
| 879 | WL_DISPLAY_ERROR_INVALID_OBJECT, |
| 880 | "incompatible version, server is %d " "client wants %d", |
| 881 | XDG_SERVER_VERSION, args[0].i); |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | wl_resource_set_implementation(resource, |
| 886 | &weston_desktop_xdg_shell_implementation, |
| 887 | client, implementation); |
| 888 | |
| 889 | return 1; |
| 890 | } |
| 891 | |
| 892 | static void |
| 893 | weston_desktop_xdg_shell_bind(struct wl_client *client, void *data, |
| 894 | uint32_t version, uint32_t id) |
| 895 | { |
| 896 | struct weston_desktop *desktop = data; |
| 897 | |
| 898 | weston_desktop_client_create(desktop, client, |
| 899 | xdg_shell_unversioned_dispatch, |
| 900 | &xdg_shell_interface, NULL, version, id); |
| 901 | } |
| 902 | |
| 903 | struct wl_global * |
| 904 | weston_desktop_xdg_shell_v5_create(struct weston_desktop *desktop, |
| 905 | struct wl_display *display) |
| 906 | { |
| 907 | return wl_global_create(display, |
| 908 | &xdg_shell_interface, |
| 909 | WD_XDG_SHELL_PROTOCOL_VERSION, |
| 910 | desktop, weston_desktop_xdg_shell_bind); |
| 911 | } |