diff --git a/library/linux/src/embedder.cc b/library/linux/src/embedder.cc index 41e36ef97..bcdd29493 100644 --- a/library/linux/src/embedder.cc +++ b/library/linux/src/embedder.cc @@ -26,6 +26,7 @@ #include +#include "library/linux/src/internal/key_event_plugin.h" #include "library/linux/src/internal/keyboard_hook_handler.h" #include "library/linux/src/internal/plugin_handler.h" #include "library/linux/src/internal/text_input_plugin.h" @@ -275,13 +276,17 @@ GLFWwindow *CreateFlutterWindow(size_t initial_width, size_t initial_height, FlutterEmbedderState *state = new FlutterEmbedderState(); state->plugin_handler = std::make_unique(engine); state->engine = engine; + + auto key_event_plugin = std::make_unique(); + state->keyboard_hook_handlers.push_back(key_event_plugin.get()); auto input_plugin = std::make_unique(); state->keyboard_hook_handlers.push_back(input_plugin.get()); - + glfwSetWindowUserPointer(window, state); + AddPlugin(window, std::move(key_event_plugin)); AddPlugin(window, std::move(input_plugin)); - + int width, height; glfwGetWindowSize(window, &width, &height); GLFWwindowSizeCallback(window, width, height); diff --git a/library/linux/src/internal/key_event_plugin.cc b/library/linux/src/internal/key_event_plugin.cc new file mode 100644 index 000000000..05485fc6f --- /dev/null +++ b/library/linux/src/internal/key_event_plugin.cc @@ -0,0 +1,60 @@ +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "library/linux/src/internal/key_event_plugin.h" +#include +#include + +static constexpr char kChannelName[] = "flutter/keyevent"; + +static constexpr char kKeyCodeKey[] = "keyCode"; +static constexpr char kKeyMapKey[] = "keymap"; +static constexpr char kTypeKey[] = "type"; + +static constexpr char kAndroidKeyMap[] = "android"; +static constexpr char kKeyUp[] = "keyup"; +static constexpr char kKeyDown[] = "keydown"; +// TODO: This event is not supported by Flutter. Add once it's implemented. +static constexpr char kRepeat[] = "repeat"; + +namespace flutter_desktop_embedding { +KeyEventPlugin::KeyEventPlugin() : JsonPlugin(kChannelName, false) {} + +KeyEventPlugin::~KeyEventPlugin() {} + +void KeyEventPlugin::CharHook(GLFWwindow *window, unsigned int code_point) {} + +void KeyEventPlugin::KeyboardHook(GLFWwindow *window, int key, int scancode, + int action, int mods) { + Json::Value args; + args[kKeyCodeKey] = key; + args[kKeyMapKey] = kAndroidKeyMap; + switch (action) { + case GLFW_PRESS: + args[kTypeKey] = kKeyDown; + break; + case GLFW_RELEASE: + args[kTypeKey] = kKeyUp; + break; + default: + break; + } + /// Messages to flutter/keyevent channels have no method name. + InvokeMethod("", args); +} + +void KeyEventPlugin::HandleJsonMethodCall( + const JsonMethodCall &method_call, std::unique_ptr result) { + // There are no methods invoked from Flutter on this channel. + } +} // namespace flutter_desktop_embedding \ No newline at end of file diff --git a/library/linux/src/internal/key_event_plugin.h b/library/linux/src/internal/key_event_plugin.h new file mode 100644 index 000000000..0ab992f5a --- /dev/null +++ b/library/linux/src/internal/key_event_plugin.h @@ -0,0 +1,39 @@ +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef LIBRARY_LINUX_SRC_INTERNAL_KEY_EVENT_PLUGIN_H_ +#define LIBRARY_LINUX_SRC_INTERNAL_kEY_EVENT_PLUGIN_H_ + +#include "library/linux/include/flutter_desktop_embedding/json_plugin.h" +#include "library/linux/src/internal/keyboard_hook_handler.h" + +namespace flutter_desktop_embedding { +class KeyEventPlugin : public KeyboardHookHandler, public JsonPlugin { + public: + KeyEventPlugin(); + virtual ~KeyEventPlugin(); + + // Plugin. + void HandleJsonMethodCall(const JsonMethodCall &method_call, + std::unique_ptr result) override; + + // KeyboardHookHandler. + void KeyboardHook(GLFWwindow *window, int key, int scancode, int action, + int mods) override; + + // KeyboardHookHandler. + void CharHook(GLFWwindow *window, unsigned int code_point) override; +}; +} // namespace flutter_desktop_embedding + +#endif // LIBRARY_LINUX_SRC_INTERNAL_KEY_EVENT_PLUGIN_H_ diff --git a/library/linux/src/json_method_codec.cc b/library/linux/src/json_method_codec.cc index 29107110f..b0e98b7b0 100644 --- a/library/linux/src/json_method_codec.cc +++ b/library/linux/src/json_method_codec.cc @@ -49,6 +49,10 @@ std::unique_ptr JsonMethodCodec::DecodeMethodCallInternal( std::unique_ptr> JsonMethodCodec::EncodeMethodCallInternal( const MethodCall &method_call) const { Json::Value message(Json::objectValue); + if (method_call.method_name().empty()) { + return EncodeJsonObject( + *static_cast(method_call.arguments())); + } message[kMessageMethodKey] = method_call.method_name(); message[kMessageArgumentsKey] = *static_cast(method_call.arguments());