提交 30e4e587 编写于 作者: A Adam Barth

Add raw keyboard support to glfw (#2601)

This patch maps glfw's keyboard input to the raw keyboard Mojo interface.
上级 48de25e1
...@@ -440,6 +440,7 @@ if (is_android) { ...@@ -440,6 +440,7 @@ if (is_android) {
] ]
deps += [ deps += [
"//sky/services/raw_keyboard:interfaces",
"//third_party/glfw", "//third_party/glfw",
"//ui/gl", "//ui/gl",
] ]
......
...@@ -29,12 +29,43 @@ void OnCursorPosChanged(GLFWwindow* window, double x, double y) { ...@@ -29,12 +29,43 @@ void OnCursorPosChanged(GLFWwindow* window, double x, double y) {
ToImpl(window)->DispatchMouseMoveEvent(x, y); ToImpl(window)->DispatchMouseMoveEvent(x, y);
} }
void OnKeyEvent(GLFWwindow* window, int key, int scancode, int action,
int mods) {
sky::InputEventPtr result = sky::InputEvent::New();
result->time_stamp = base::TimeTicks::Now().ToInternalValue();
switch (action) {
case GLFW_PRESS:
case GLFW_REPEAT:
result->type = sky::EventType::KEY_PRESSED;
break;
case GLFW_RELEASE:
result->type = sky::EventType::KEY_RELEASED;
break;
default:
LOG(WARNING) << "Unknown key action: " << action;
return;
}
result->key_data = sky::KeyData::New();
result->key_data->key_code = key;
result->key_data->scan_code = scancode;
if (mods & GLFW_MOD_SHIFT)
result->key_data->meta_state |= 0x00000001;
if (mods & GLFW_MOD_CONTROL)
result->key_data->meta_state |= 0x00001000;
if (mods & GLFW_MOD_ALT)
result->key_data->meta_state |= 0x00000002;
ToImpl(window)->DispatchKeyEvent(result.Pass());
}
} // namespace } // namespace
WindowImpl::WindowImpl(GLFWwindow* window) WindowImpl::WindowImpl(GLFWwindow* window)
: window_(window), : window_(window),
shell_view_(new ShellView(Shell::Shared())), shell_view_(new ShellView(Shell::Shared())),
buttons_(0) { buttons_(0),
view_services_binding_(this) {
glfwSetWindowUserPointer(window_, this); glfwSetWindowUserPointer(window_, this);
auto platform_view = auto platform_view =
static_cast<sky::shell::PlatformViewGLFW*>(shell_view_->view()); static_cast<sky::shell::PlatformViewGLFW*>(shell_view_->view());
...@@ -48,6 +79,7 @@ WindowImpl::WindowImpl(GLFWwindow* window) ...@@ -48,6 +79,7 @@ WindowImpl::WindowImpl(GLFWwindow* window)
glfwSetWindowSizeCallback(window_, OnWindowSizeChanged); glfwSetWindowSizeCallback(window_, OnWindowSizeChanged);
glfwSetMouseButtonCallback(window_, OnMouseButtonChanged); glfwSetMouseButtonCallback(window_, OnMouseButtonChanged);
glfwSetKeyCallback(window_, OnKeyEvent);
} }
WindowImpl::~WindowImpl() { WindowImpl::~WindowImpl() {
...@@ -58,6 +90,12 @@ WindowImpl::~WindowImpl() { ...@@ -58,6 +90,12 @@ WindowImpl::~WindowImpl() {
void WindowImpl::RunFromBundle(const std::string& script_uri, void WindowImpl::RunFromBundle(const std::string& script_uri,
const std::string& bundle_path) { const std::string& bundle_path) {
mojo::ServiceProviderPtr view_services;
view_services_binding_.Bind(mojo::GetProxy(&view_services));
ServicesDataPtr services = ServicesData::New();
services->view_services = view_services.Pass();
engine_->SetServices(services.Pass());
engine_->RunFromBundle(script_uri, bundle_path); engine_->RunFromBundle(script_uri, bundle_path);
} }
...@@ -135,5 +173,26 @@ void WindowImpl::DispatchMouseMoveEvent(double x, double y) { ...@@ -135,5 +173,26 @@ void WindowImpl::DispatchMouseMoveEvent(double x, double y) {
engine_->OnPointerPacket(pointer_packet.Pass()); engine_->OnPointerPacket(pointer_packet.Pass());
} }
void WindowImpl::DispatchKeyEvent(sky::InputEventPtr sky_event) {
for (auto& listener : raw_keyboard_listeners_) {
listener->OnKey(sky_event.Clone());
}
}
void WindowImpl::ConnectToService(const mojo::String& service_name,
mojo::ScopedMessagePipeHandle handle) {
if (service_name == raw_keyboard::RawKeyboardService::Name_) {
raw_keyboard_bindings_.AddBinding(
this,
mojo::MakeRequest<raw_keyboard::RawKeyboardService>(handle.Pass()));
}
}
void WindowImpl::AddListener(
mojo::InterfaceHandle<raw_keyboard::RawKeyboardListener> listener) {
raw_keyboard_listeners_.push_back(
raw_keyboard::RawKeyboardListenerPtr::Create(listener.Pass()));
}
} // namespace shell } // namespace shell
} // namespace sky } // namespace sky
...@@ -8,17 +8,20 @@ ...@@ -8,17 +8,20 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "mojo/common/binding_set.h"
#include "sky/services/engine/sky_engine.mojom.h" #include "sky/services/engine/sky_engine.mojom.h"
#include "sky/services/raw_keyboard/raw_keyboard.mojom.h"
#include "sky/shell/platform_view.h" #include "sky/shell/platform_view.h"
#include "sky/shell/shell_view.h" #include "sky/shell/shell_view.h"
namespace sky { namespace sky {
namespace shell { namespace shell {
class WindowImpl { class WindowImpl : public mojo::ServiceProvider,
public raw_keyboard::RawKeyboardService {
public: public:
explicit WindowImpl(GLFWwindow* window); explicit WindowImpl(GLFWwindow* window);
~WindowImpl(); ~WindowImpl() override;
void RunFromBundle(const std::string& script_uri, void RunFromBundle(const std::string& script_uri,
const std::string& bundle_path); const std::string& bundle_path);
...@@ -26,6 +29,16 @@ class WindowImpl { ...@@ -26,6 +29,16 @@ class WindowImpl {
void UpdateViewportMetrics(int width, int height); void UpdateViewportMetrics(int width, int height);
void DispatchMouseButtonEvent(int button, int action, int mods); void DispatchMouseButtonEvent(int button, int action, int mods);
void DispatchMouseMoveEvent(double x, double y); void DispatchMouseMoveEvent(double x, double y);
void DispatchKeyEvent(sky::InputEventPtr event);
// mojo::ServiceProvider
void ConnectToService(const mojo::String& service_name,
mojo::ScopedMessagePipeHandle client_handle) override;
// raw_keyboard::RawKeyboardService
void AddListener(
mojo::InterfaceHandle<raw_keyboard::RawKeyboardListener> listener)
override;
private: private:
GLFWwindow* window_; GLFWwindow* window_;
...@@ -33,6 +46,10 @@ class WindowImpl { ...@@ -33,6 +46,10 @@ class WindowImpl {
sky::SkyEnginePtr engine_; sky::SkyEnginePtr engine_;
int buttons_; int buttons_;
mojo::Binding<mojo::ServiceProvider> view_services_binding_;
mojo::BindingSet<raw_keyboard::RawKeyboardService> raw_keyboard_bindings_;
std::vector<raw_keyboard::RawKeyboardListenerPtr> raw_keyboard_listeners_;
DISALLOW_COPY_AND_ASSIGN(WindowImpl); DISALLOW_COPY_AND_ASSIGN(WindowImpl);
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册