From d8ed73e0a924ac9c2da966f1f3eb7dd5127a29a1 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 18 Mar 2016 14:13:12 -0700 Subject: [PATCH] Wire up raw keyboard events in Mozart --- sky/shell/platform/mojo/BUILD.gn | 1 + sky/shell/platform/mojo/view_impl.cc | 61 +++++++++++++++++++++++++++- sky/shell/platform/mojo/view_impl.h | 20 ++++++++- 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/sky/shell/platform/mojo/BUILD.gn b/sky/shell/platform/mojo/BUILD.gn index 9cd4848c798..ac884a8872d 100644 --- a/sky/shell/platform/mojo/BUILD.gn +++ b/sky/shell/platform/mojo/BUILD.gn @@ -43,6 +43,7 @@ mojo_native_application("mojo") { "//sky/engine/public/sky", "//sky/engine/tonic", "//sky/engine/web", + "//sky/services/raw_keyboard:interfaces", "//sky/shell:common", "//sky/shell:gpu_mojo", "//third_party/icu", diff --git a/sky/shell/platform/mojo/view_impl.cc b/sky/shell/platform/mojo/view_impl.cc index 8ff54cafb88..3c43abacddf 100644 --- a/sky/shell/platform/mojo/view_impl.cc +++ b/sky/shell/platform/mojo/view_impl.cc @@ -10,11 +10,44 @@ namespace sky { namespace shell { +namespace { + +sky::InputEventPtr ConvertKeyEvent(mojo::EventPtr event) { + if (!event->key_data) + return nullptr; + sky::InputEventPtr result = sky::InputEvent::New(); + result->time_stamp = event->time_stamp; + switch (event->action) { + case mojo::EventType::KEY_PRESSED: + result->type = sky::EventType::KEY_PRESSED; + break; + case mojo::EventType::KEY_RELEASED: + result->type = sky::EventType::KEY_RELEASED; + break; + default: + return nullptr; + } + result->key_data = sky::KeyData::New(); + result->key_data->key_code = event->key_data->key_code; + if (static_cast(event->flags) & static_cast(mojo::EventFlags::SHIFT_DOWN)) + result->key_data->meta_state |= 0x00000001; + if (static_cast(event->flags) & static_cast(mojo::EventFlags::CONTROL_DOWN)) + result->key_data->meta_state |= 0x00001000; + if (static_cast(event->flags) & static_cast(mojo::EventFlags::ALT_DOWN)) + result->key_data->meta_state |= 0x00000002; + + return result.Pass(); +} + +} // namespace ViewImpl::ViewImpl(mojo::InterfaceRequest view_owner, ServicesDataPtr services, const std::string& url) - : binding_(this), url_(url), listener_binding_(this) { + : binding_(this), + url_(url), + listener_binding_(this), + view_services_binding_(this) { DCHECK(services); // Once we're done invoking |Shell|, we put it back inside |services| and pass @@ -50,7 +83,11 @@ ViewImpl::ViewImpl(mojo::InterfaceRequest view_owner, shell->CreateApplicationConnector(mojo::GetProxy(&connector)); platform_view()->InitRasterizer(connector.Pass(), scene.Pass()); + mojo::ServiceProviderPtr view_services; + view_services_binding_.Bind(mojo::GetProxy(&view_services)); + services->shell = shell.Pass(); + services->view_services = view_services.Pass(); engine_->SetServices(services.Pass()); } @@ -96,11 +133,33 @@ void ViewImpl::OnEvent(mojo::EventPtr event, const OnEventCallback& callback) { } break; } + case mojo::EventType::KEY_PRESSED: + case mojo::EventType::KEY_RELEASED: { + auto sky_event = ConvertKeyEvent(event.Pass()); + for (auto& listener : raw_keyboard_listeners_) + listener->OnKey(sky_event.Clone()); + break; + } default: break; } callback.Run(consumed); } +void ViewImpl::ConnectToService(const mojo::String& service_name, + mojo::ScopedMessagePipeHandle handle) { + if (service_name == raw_keyboard::RawKeyboardService::Name_) { + raw_keyboard_bindings_.AddBinding( + this, + mojo::MakeRequest(handle.Pass())); + } +} + +void ViewImpl::AddListener( + mojo::InterfaceHandle listener) { + raw_keyboard_listeners_.push_back( + raw_keyboard::RawKeyboardListenerPtr::Create(listener.Pass())); +} + } // namespace shell } // namespace sky diff --git a/sky/shell/platform/mojo/view_impl.h b/sky/shell/platform/mojo/view_impl.h index 7c9c7201836..1441461a7cc 100644 --- a/sky/shell/platform/mojo/view_impl.h +++ b/sky/shell/platform/mojo/view_impl.h @@ -5,7 +5,10 @@ #ifndef SKY_SHELL_PLATFORM_MOJO_VIEW_IMPL_H_ #define SKY_SHELL_PLATFORM_MOJO_VIEW_IMPL_H_ +#include + #include "base/macros.h" +#include "mojo/common/binding_set.h" #include "mojo/public/cpp/bindings/strong_binding.h" #include "mojo/public/interfaces/application/application_connector.mojom.h" #include "mojo/services/gfx/composition/interfaces/scenes.mojom.h" @@ -13,6 +16,7 @@ #include "mojo/services/ui/views/interfaces/view_manager.mojom.h" #include "mojo/services/ui/views/interfaces/view_provider.mojom.h" #include "mojo/services/ui/views/interfaces/views.mojom.h" +#include "sky/services/raw_keyboard/raw_keyboard.mojom.h" #include "sky/shell/platform/mojo/platform_view_mojo.h" #include "sky/shell/platform/mojo/pointer_converter_mojo.h" #include "sky/shell/shell_view.h" @@ -21,7 +25,9 @@ namespace sky { namespace shell { class ViewImpl : public mojo::ui::ViewListener, - public mojo::ui::InputListener { + public mojo::ui::InputListener, + public mojo::ServiceProvider, + public raw_keyboard::RawKeyboardService { public: ViewImpl(mojo::InterfaceRequest view_owner, ServicesDataPtr services, @@ -41,6 +47,15 @@ class ViewImpl : public mojo::ui::ViewListener, // mojo::ui::InputListener void OnEvent(mojo::EventPtr event, const OnEventCallback& callback) override; + // mojo::ServiceProvider + void ConnectToService(const mojo::String& service_name, + mojo::ScopedMessagePipeHandle client_handle) override; + + // raw_keyboard::RawKeyboardService + void AddListener( + mojo::InterfaceHandle listener) + override; + PlatformViewMojo* platform_view() { return static_cast(shell_view_->view()); } @@ -51,6 +66,9 @@ class ViewImpl : public mojo::ui::ViewListener, mojo::ServiceProviderPtr view_service_provider_; mojo::ui::InputConnectionPtr input_connection_; mojo::Binding listener_binding_; + mojo::Binding view_services_binding_; + mojo::BindingSet raw_keyboard_bindings_; + std::vector raw_keyboard_listeners_; std::unique_ptr shell_view_; SkyEnginePtr engine_;