Chromium Code Reviews| Index: ui/ozone/platform/wayland/fake_server.cc |
| diff --git a/ui/ozone/platform/wayland/fake_server.cc b/ui/ozone/platform/wayland/fake_server.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8a7378773e24d1c42381f2e83dbdcc70273ff7c8 |
| --- /dev/null |
| +++ b/ui/ozone/platform/wayland/fake_server.cc |
| @@ -0,0 +1,326 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/ozone/platform/wayland/fake_server.h" |
| + |
| +#include <sys/socket.h> |
| +#include <wayland-server.h> |
| +#include <xdg-shell-unstable-v5-server-protocol.h> |
| + |
| +#include "base/bind.h" |
| +#include "base/files/scoped_file.h" |
| + |
| +namespace wl { |
| +namespace { |
| + |
| +template <class T> |
| +void Delete(wl_resource* resource) { |
|
spang
2016/02/10 18:58:29
DeleteResourceData?
|
| + delete static_cast<T*>(wl_resource_get_user_data(resource)); |
| +} |
| + |
| +void DestroyResource(wl_client* client, wl_resource* resource) { |
| + wl_resource_destroy(resource); |
| +} |
| + |
| +// wl_surface |
| + |
| +void Attach(wl_client* client, |
| + wl_resource* resource, |
| + wl_resource* buffer_resource, |
| + int32_t x, |
| + int32_t y) { |
| + auto obj = static_cast<FakeSurface*>(wl_resource_get_user_data(resource)); |
| + if (obj->mock) |
| + obj->mock->Attach(buffer_resource, x, y); |
| +} |
| + |
| +void Damage(wl_client* client, |
| + wl_resource* resource, |
| + int32_t x, |
| + int32_t y, |
| + int32_t width, |
| + int32_t height) { |
| + auto obj = static_cast<FakeSurface*>(wl_resource_get_user_data(resource)); |
| + if (obj->mock) |
| + obj->mock->Damage(x, y, width, height); |
| +} |
| + |
| +void Commit(wl_client* client, wl_resource* resource) { |
| + auto obj = static_cast<FakeSurface*>(wl_resource_get_user_data(resource)); |
| + if (obj->mock) |
| + obj->mock->Commit(); |
| +} |
| + |
| +const struct wl_surface_interface surface_impl = { |
| + &DestroyResource, &Attach, &Damage, |
|
spang
2016/02/10 18:58:29
Can you annotate all of them for consistency?
Michael Forney
2016/02/11 03:38:09
Done.
|
| + nullptr, // frame |
| + nullptr, // set_opaque_region |
| + nullptr, // set_input_region |
| + &Commit, |
| + nullptr, // set_buffer_transform |
| + nullptr, // set_buffer_scale |
| +}; |
| + |
| +// wl_compositor |
| + |
| +void CreateSurface(wl_client* client, wl_resource* resource, uint32_t id) { |
| + wl_resource* surface_resource = |
| + wl_resource_create(client, &wl_surface_interface, 4, id); |
|
spang
2016/02/10 18:58:28
What is 4? Maybe do
static const int kWhatIsF
Michael Forney
2016/02/11 03:38:09
Done.
|
| + if (!surface_resource) { |
| + wl_client_post_no_memory(client); |
| + return; |
| + } |
| + wl_resource_set_implementation(surface_resource, &surface_impl, |
| + new FakeSurface(surface_resource), |
| + &Delete<FakeSurface>); |
| +} |
| + |
| +const struct wl_compositor_interface compositor_impl = { |
| + &CreateSurface, |
| + nullptr, // create_region |
| +}; |
| + |
| +// xdg_surface |
| + |
| +void SetTitle(wl_client* client, wl_resource* resource, const char* title) { |
| + auto obj = static_cast<FakeXdgSurface*>(wl_resource_get_user_data(resource)); |
| + if (obj->mock) |
|
spang
2016/02/10 18:58:29
When is this false?
Michael Forney
2016/02/11 03:38:09
It could be NULL if the implementation makes a req
spang
2016/02/11 16:54:48
Wouldn't forgetting to set the mock immediately be
Michael Forney
2016/02/12 00:49:58
The client essentially owns the server-side FakeSu
|
| + obj->mock->SetTitle(title); |
| +} |
| + |
| +void SetAppId(wl_client* client, wl_resource* resource, const char* app_id) { |
| + auto obj = static_cast<FakeXdgSurface*>(wl_resource_get_user_data(resource)); |
| + if (obj->mock) |
| + obj->mock->SetAppId(app_id); |
| +} |
| + |
| +void AckConfigure(wl_client* client, wl_resource* resource, uint32_t serial) { |
| + auto obj = static_cast<FakeXdgSurface*>(wl_resource_get_user_data(resource)); |
| + if (obj->mock) |
| + obj->mock->AckConfigure(serial); |
| +} |
| + |
| +void SetMaximized(wl_client* client, wl_resource* resource) { |
| + auto obj = static_cast<FakeXdgSurface*>(wl_resource_get_user_data(resource)); |
| + if (obj->mock) |
| + obj->mock->SetMaximized(); |
| +} |
| + |
| +void UnsetMaximized(wl_client* client, wl_resource* resource) { |
| + auto obj = static_cast<FakeXdgSurface*>(wl_resource_get_user_data(resource)); |
| + if (obj->mock) |
| + obj->mock->UnsetMaximized(); |
| +} |
| + |
| +void SetMinimized(wl_client* client, wl_resource* resource) { |
| + auto obj = static_cast<FakeXdgSurface*>(wl_resource_get_user_data(resource)); |
| + if (obj->mock) |
| + obj->mock->SetMinimized(); |
| +} |
| + |
| +const struct xdg_surface_interface xdg_surface_impl = { |
| + &DestroyResource, |
| + nullptr, // set_parent |
| + &SetTitle, &SetAppId, |
| + nullptr, // show_window_menu |
| + nullptr, // move |
| + nullptr, // resize |
| + &AckConfigure, |
| + nullptr, // set_window_geometry |
| + &SetMaximized, &UnsetMaximized, |
| + nullptr, // set_fullscreen |
| + nullptr, // unset_fullscreen |
| + &SetMinimized, |
| +}; |
| + |
| +// xdg_shell |
| + |
| +void UseUnstableVersion(wl_client* client, |
| + wl_resource* resource, |
| + int32_t version) { |
| + auto mock = static_cast<MockXdgShell*>(wl_resource_get_user_data(resource)); |
| + if (mock) |
| + mock->UseUnstableVersion(version); |
| +} |
| + |
| +void GetXdgSurface(wl_client* client, |
| + wl_resource* resource, |
| + uint32_t id, |
| + wl_resource* surface_resource) { |
| + auto surface = |
| + static_cast<FakeSurface*>(wl_resource_get_user_data(surface_resource)); |
| + if (surface->xdg_surface) { |
| + wl_resource_post_error(resource, XDG_SHELL_ERROR_ROLE, |
| + "surface already has a role"); |
| + return; |
| + } |
| + wl_resource* xdg_surface_resource = |
| + wl_resource_create(client, &xdg_surface_interface, 1, id); |
| + if (!xdg_surface_resource) { |
| + wl_client_post_no_memory(client); |
| + return; |
| + } |
| + wl_resource_set_implementation( |
| + xdg_surface_resource, &xdg_surface_impl, |
| + new FakeXdgSurface(xdg_surface_resource, surface), |
| + &Delete<FakeXdgSurface>); |
| +} |
| + |
| +void Pong(wl_client* client, wl_resource* resource, uint32_t serial) { |
| + auto mock = static_cast<MockXdgShell*>(wl_resource_get_user_data(resource)); |
| + if (mock) |
| + mock->Pong(serial); |
| +} |
| + |
| +const struct xdg_shell_interface xdg_shell_impl = { |
| + &DestroyResource, |
| + &UseUnstableVersion, |
| + &GetXdgSurface, |
| + nullptr, // get_xdg_popup |
| + &Pong, |
| +}; |
| + |
| +} // namespace |
| + |
| +MockSurface::MockSurface() {} |
| + |
| +MockSurface::~MockSurface() {} |
| + |
| +MockXdgShell::MockXdgShell() {} |
| + |
| +MockXdgShell::~MockXdgShell() {} |
| + |
| +MockXdgSurface::MockXdgSurface() {} |
| + |
| +MockXdgSurface::~MockXdgSurface() {} |
| + |
| +FakeSurface::FakeSurface(wl_resource* resource) : resource(resource) {} |
| + |
| +FakeSurface::~FakeSurface() { |
| + if (xdg_surface) |
| + wl_resource_destroy(xdg_surface->resource); |
| +} |
| + |
| +// static |
| +FakeSurface* FakeSurface::FromResource(wl_resource* resource) { |
| + if (!wl_resource_instance_of(resource, &wl_surface_interface, &surface_impl)) |
| + return nullptr; |
| + return static_cast<FakeSurface*>(wl_resource_get_user_data(resource)); |
| +} |
| + |
| +FakeXdgSurface::FakeXdgSurface(wl_resource* resource, FakeSurface* surface) |
| + : resource(resource), surface(surface) { |
| + surface->xdg_surface = this; |
| +} |
| + |
| +FakeXdgSurface::~FakeXdgSurface() { |
| + surface->xdg_surface = nullptr; |
| +} |
| + |
| +void GlobalDeleter::operator()(wl_global* global) { |
| + wl_global_destroy(global); |
| +} |
| + |
| +Global::Global(const wl_interface* interface, |
| + const void* implementation, |
| + uint32_t version, |
| + void* data) |
| + : interface_(interface), |
| + implementation_(implementation), |
| + version_(version), |
| + data_(data) {} |
| + |
| +Global::~Global() {} |
| + |
| +bool Global::Initialize(wl_display* display) { |
| + global_.reset(wl_global_create(display, interface_, version_, this, &Bind)); |
| + return global_; |
| +} |
| + |
| +// static |
| +void Global::Bind(wl_client* client, |
| + void* data, |
| + uint32_t version, |
| + uint32_t id) { |
| + auto global = static_cast<Global*>(data); |
| + wl_resource* resource = |
| + wl_resource_create(client, global->interface_, global->version_, id); |
| + if (!resource) { |
| + wl_client_post_no_memory(client); |
| + return; |
| + } |
| + if (!global->resource_) |
| + global->resource_ = resource; |
| + wl_resource_set_implementation(resource, global->implementation_, |
| + global->data_, nullptr); |
| +} |
| + |
| +void DisplayDeleter::operator()(wl_display* display) { |
| + wl_display_destroy(display); |
| +} |
| + |
| +FakeServer::FakeServer(MockXdgShell* xdg_shell_mock) |
| + : Thread("fake_wayland_server"), |
| + compositor_(&wl_compositor_interface, &compositor_impl, 4, nullptr), |
| + xdg_shell_(&xdg_shell_interface, &xdg_shell_impl, 1, xdg_shell_mock) {} |
| + |
| +FakeServer::~FakeServer() { |
| + Stop(); |
| +} |
| + |
| +bool FakeServer::Start() { |
| + display_.reset(wl_display_create()); |
| + if (!display_) |
| + return false; |
| + event_loop_ = wl_display_get_event_loop(display_.get()); |
| + |
| + int fd[2]; |
| + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fd) < 0) |
| + return false; |
| + base::ScopedFD server_fd(fd[0]); |
| + base::ScopedFD client_fd(fd[1]); |
| + |
| + if (wl_display_init_shm(display_.get()) < 0) |
| + return false; |
| + if (!compositor_.Initialize(display_.get())) |
| + return false; |
| + if (!xdg_shell_.Initialize(display_.get())) |
| + return false; |
| + |
| + client_ = wl_client_create(display_.get(), server_fd.get()); |
| + if (!client_) |
| + return false; |
| + (void)server_fd.release(); |
| + |
| + base::Thread::Options options; |
| + options.message_pump_factory = |
| + base::Bind(&FakeServer::CreateMessagePump, base::Unretained(this)); |
| + if (!base::Thread::StartWithOptions(options)) |
| + return false; |
| + |
| + setenv("WAYLAND_SOCKET", std::to_string(client_fd.release()).c_str(), 1); |
|
spang
2016/02/10 18:58:29
std::to_string not allowed yet
See https://chromi
Michael Forney
2016/02/11 03:38:09
Done.
|
| + |
| + return true; |
| +} |
| + |
| +void FakeServer::Flush() { |
| + wl_display_flush_clients(display_.get()); |
| +} |
| + |
| +scoped_ptr<base::MessagePump> FakeServer::CreateMessagePump() { |
| + auto pump = make_scoped_ptr(new base::MessagePumpLibevent); |
| + pump->WatchFileDescriptor(wl_event_loop_get_fd(event_loop_), true, |
| + base::MessagePumpLibevent::WATCH_READ, &controller_, |
| + this); |
| + return std::move(pump); |
| +} |
| + |
| +void FakeServer::OnFileCanReadWithoutBlocking(int fd) { |
| + wl_event_loop_dispatch(event_loop_, 0); |
| + wl_display_flush_clients(display_.get()); |
| +} |
| + |
| +void FakeServer::OnFileCanWriteWithoutBlocking(int fd) {} |
| + |
| +} // namespace wl |