| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/ozone/platform/wayland/fake_server.h" | 5 #include "ui/ozone/platform/wayland/fake_server.h" |
| 6 | 6 |
| 7 #include <sys/socket.h> | 7 #include <sys/socket.h> |
| 8 #include <wayland-server.h> | 8 #include <wayland-server.h> |
| 9 #include <xdg-shell-unstable-v5-server-protocol.h> | 9 #include <xdg-shell-unstable-v5-server-protocol.h> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/files/scoped_file.h" | 12 #include "base/files/scoped_file.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 | 15 |
| 16 namespace wl { | 16 namespace wl { |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 const uint32_t kCompositorVersion = 4; | 19 const uint32_t kCompositorVersion = 4; |
| 20 const uint32_t kSeatVersion = 4; |
| 20 const uint32_t kXdgShellVersion = 1; | 21 const uint32_t kXdgShellVersion = 1; |
| 21 | 22 |
| 22 void DestroyResource(wl_client* client, wl_resource* resource) { | 23 void DestroyResource(wl_client* client, wl_resource* resource) { |
| 23 wl_resource_destroy(resource); | 24 wl_resource_destroy(resource); |
| 24 } | 25 } |
| 25 | 26 |
| 26 // wl_compositor | 27 // wl_compositor |
| 27 | 28 |
| 28 void CreateSurface(wl_client* client, wl_resource* resource, uint32_t id) { | 29 void CreateSurface(wl_client* client, wl_resource* resource, uint32_t id) { |
| 29 auto compositor = | 30 auto compositor = |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 } | 115 } |
| 115 | 116 |
| 116 const struct xdg_shell_interface xdg_shell_impl = { | 117 const struct xdg_shell_interface xdg_shell_impl = { |
| 117 &DestroyResource, // destroy | 118 &DestroyResource, // destroy |
| 118 &UseUnstableVersion, // use_unstable_version | 119 &UseUnstableVersion, // use_unstable_version |
| 119 &GetXdgSurface, // get_xdg_surface | 120 &GetXdgSurface, // get_xdg_surface |
| 120 nullptr, // get_xdg_popup | 121 nullptr, // get_xdg_popup |
| 121 &Pong, // pong | 122 &Pong, // pong |
| 122 }; | 123 }; |
| 123 | 124 |
| 125 // wl_seat |
| 126 |
| 127 void GetPointer(wl_client* client, wl_resource* resource, uint32_t id) { |
| 128 auto seat = static_cast<MockSeat*>(wl_resource_get_user_data(resource)); |
| 129 wl_resource* pointer_resource = wl_resource_create( |
| 130 client, &wl_pointer_interface, wl_resource_get_version(resource), id); |
| 131 if (!pointer_resource) { |
| 132 wl_client_post_no_memory(client); |
| 133 return; |
| 134 } |
| 135 seat->pointer.reset(new MockPointer(pointer_resource)); |
| 136 } |
| 137 |
| 138 const struct wl_seat_interface seat_impl = { |
| 139 &GetPointer, // get_pointer |
| 140 nullptr, // get_keyboard |
| 141 nullptr, // get_touch, |
| 142 &DestroyResource, // release |
| 143 }; |
| 144 |
| 145 // wl_pointer |
| 146 |
| 147 const struct wl_pointer_interface pointer_impl = { |
| 148 nullptr, // set_cursor |
| 149 &DestroyResource, // release |
| 150 }; |
| 151 |
| 124 // xdg_surface | 152 // xdg_surface |
| 125 | 153 |
| 126 void SetTitle(wl_client* client, wl_resource* resource, const char* title) { | 154 void SetTitle(wl_client* client, wl_resource* resource, const char* title) { |
| 127 static_cast<MockXdgSurface*>(wl_resource_get_user_data(resource)) | 155 static_cast<MockXdgSurface*>(wl_resource_get_user_data(resource)) |
| 128 ->SetTitle(title); | 156 ->SetTitle(title); |
| 129 } | 157 } |
| 130 | 158 |
| 131 void SetAppId(wl_client* client, wl_resource* resource, const char* app_id) { | 159 void SetAppId(wl_client* client, wl_resource* resource, const char* app_id) { |
| 132 static_cast<MockXdgSurface*>(wl_resource_get_user_data(resource)) | 160 static_cast<MockXdgSurface*>(wl_resource_get_user_data(resource)) |
| 133 ->SetAppId(app_id); | 161 ->SetAppId(app_id); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 if (xdg_surface && xdg_surface->resource()) | 229 if (xdg_surface && xdg_surface->resource()) |
| 202 wl_resource_destroy(xdg_surface->resource()); | 230 wl_resource_destroy(xdg_surface->resource()); |
| 203 } | 231 } |
| 204 | 232 |
| 205 MockSurface* MockSurface::FromResource(wl_resource* resource) { | 233 MockSurface* MockSurface::FromResource(wl_resource* resource) { |
| 206 if (!wl_resource_instance_of(resource, &wl_surface_interface, &surface_impl)) | 234 if (!wl_resource_instance_of(resource, &wl_surface_interface, &surface_impl)) |
| 207 return nullptr; | 235 return nullptr; |
| 208 return static_cast<MockSurface*>(wl_resource_get_user_data(resource)); | 236 return static_cast<MockSurface*>(wl_resource_get_user_data(resource)); |
| 209 } | 237 } |
| 210 | 238 |
| 239 MockPointer::MockPointer(wl_resource* resource) : ServerObject(resource) { |
| 240 wl_resource_set_implementation(resource, &pointer_impl, this, |
| 241 &ServerObject::OnResourceDestroyed); |
| 242 } |
| 243 |
| 244 MockPointer::~MockPointer() {} |
| 245 |
| 211 void GlobalDeleter::operator()(wl_global* global) { | 246 void GlobalDeleter::operator()(wl_global* global) { |
| 212 wl_global_destroy(global); | 247 wl_global_destroy(global); |
| 213 } | 248 } |
| 214 | 249 |
| 215 Global::Global(const wl_interface* interface, | 250 Global::Global(const wl_interface* interface, |
| 216 const void* implementation, | 251 const void* implementation, |
| 217 uint32_t version) | 252 uint32_t version) |
| 218 : interface_(interface), | 253 : interface_(interface), |
| 219 implementation_(implementation), | 254 implementation_(implementation), |
| 220 version_(version) {} | 255 version_(version) {} |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 | 288 |
| 254 MockCompositor::MockCompositor() | 289 MockCompositor::MockCompositor() |
| 255 : Global(&wl_compositor_interface, &compositor_impl, kCompositorVersion) {} | 290 : Global(&wl_compositor_interface, &compositor_impl, kCompositorVersion) {} |
| 256 | 291 |
| 257 MockCompositor::~MockCompositor() {} | 292 MockCompositor::~MockCompositor() {} |
| 258 | 293 |
| 259 void MockCompositor::AddSurface(scoped_ptr<MockSurface> surface) { | 294 void MockCompositor::AddSurface(scoped_ptr<MockSurface> surface) { |
| 260 surfaces_.push_back(std::move(surface)); | 295 surfaces_.push_back(std::move(surface)); |
| 261 } | 296 } |
| 262 | 297 |
| 298 MockSeat::MockSeat() : Global(&wl_seat_interface, &seat_impl, kSeatVersion) {} |
| 299 |
| 300 MockSeat::~MockSeat() {} |
| 301 |
| 263 MockXdgShell::MockXdgShell() | 302 MockXdgShell::MockXdgShell() |
| 264 : Global(&xdg_shell_interface, &xdg_shell_impl, kXdgShellVersion) {} | 303 : Global(&xdg_shell_interface, &xdg_shell_impl, kXdgShellVersion) {} |
| 265 | 304 |
| 266 MockXdgShell::~MockXdgShell() {} | 305 MockXdgShell::~MockXdgShell() {} |
| 267 | 306 |
| 268 void DisplayDeleter::operator()(wl_display* display) { | 307 void DisplayDeleter::operator()(wl_display* display) { |
| 269 wl_display_destroy(display); | 308 wl_display_destroy(display); |
| 270 } | 309 } |
| 271 | 310 |
| 272 FakeServer::FakeServer() | 311 FakeServer::FakeServer() |
| (...skipping 15 matching lines...) Expand all Loading... |
| 288 int fd[2]; | 327 int fd[2]; |
| 289 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fd) < 0) | 328 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fd) < 0) |
| 290 return false; | 329 return false; |
| 291 base::ScopedFD server_fd(fd[0]); | 330 base::ScopedFD server_fd(fd[0]); |
| 292 base::ScopedFD client_fd(fd[1]); | 331 base::ScopedFD client_fd(fd[1]); |
| 293 | 332 |
| 294 if (wl_display_init_shm(display_.get()) < 0) | 333 if (wl_display_init_shm(display_.get()) < 0) |
| 295 return false; | 334 return false; |
| 296 if (!compositor_.Initialize(display_.get())) | 335 if (!compositor_.Initialize(display_.get())) |
| 297 return false; | 336 return false; |
| 337 if (!seat_.Initialize(display_.get())) |
| 338 return false; |
| 298 if (!xdg_shell_.Initialize(display_.get())) | 339 if (!xdg_shell_.Initialize(display_.get())) |
| 299 return false; | 340 return false; |
| 300 | 341 |
| 301 client_ = wl_client_create(display_.get(), server_fd.get()); | 342 client_ = wl_client_create(display_.get(), server_fd.get()); |
| 302 if (!client_) | 343 if (!client_) |
| 303 return false; | 344 return false; |
| 304 (void)server_fd.release(); | 345 (void)server_fd.release(); |
| 305 | 346 |
| 306 base::Thread::Options options; | 347 base::Thread::Options options; |
| 307 options.message_pump_factory = | 348 options.message_pump_factory = |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 } | 382 } |
| 342 | 383 |
| 343 void FakeServer::OnFileCanReadWithoutBlocking(int fd) { | 384 void FakeServer::OnFileCanReadWithoutBlocking(int fd) { |
| 344 wl_event_loop_dispatch(event_loop_, 0); | 385 wl_event_loop_dispatch(event_loop_, 0); |
| 345 wl_display_flush_clients(display_.get()); | 386 wl_display_flush_clients(display_.get()); |
| 346 } | 387 } |
| 347 | 388 |
| 348 void FakeServer::OnFileCanWriteWithoutBlocking(int fd) {} | 389 void FakeServer::OnFileCanWriteWithoutBlocking(int fd) {} |
| 349 | 390 |
| 350 } // namespace wl | 391 } // namespace wl |
| OLD | NEW |