Skip to content

Commit fb0a08d

Browse files
[linux] Adds raw key events plugin.
1 parent ae4451d commit fb0a08d

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

library/linux/src/embedder.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <flutter_embedder.h>
2828

29+
#include "library/linux/src/internal/key_event_plugin.h"
2930
#include "library/linux/src/internal/keyboard_hook_handler.h"
3031
#include "library/linux/src/internal/plugin_handler.h"
3132
#include "library/linux/src/internal/text_input_plugin.h"
@@ -275,13 +276,17 @@ GLFWwindow *CreateFlutterWindow(size_t initial_width, size_t initial_height,
275276
FlutterEmbedderState *state = new FlutterEmbedderState();
276277
state->plugin_handler = std::make_unique<PluginHandler>(engine);
277278
state->engine = engine;
279+
280+
auto key_event_plugin = std::make_unique<KeyEventPlugin>();
281+
state->keyboard_hook_handlers.push_back(key_event_plugin.get());
278282
auto input_plugin = std::make_unique<TextInputPlugin>();
279283
state->keyboard_hook_handlers.push_back(input_plugin.get());
280-
284+
281285
glfwSetWindowUserPointer(window, state);
282286

287+
AddPlugin(window, std::move(key_event_plugin));
283288
AddPlugin(window, std::move(input_plugin));
284-
289+
285290
int width, height;
286291
glfwGetWindowSize(window, &width, &height);
287292
GLFWwindowSizeCallback(window, width, height);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#include "library/linux/src/internal/key_event_plugin.h"
15+
#include <flutter_embedder.h>
16+
#include <iostream>
17+
18+
static constexpr char kChannelName[] = "flutter/keyevent";
19+
20+
static constexpr char kKeyCodeKey[] = "keyCode";
21+
static constexpr char kKeyMapKey[] = "keymap";
22+
static constexpr char kTypeKey[] = "type";
23+
24+
static constexpr char kAndroidKeyMap[] = "android";
25+
static constexpr char kKeyUp[] = "keyup";
26+
static constexpr char kKeyDown[] = "keydown";
27+
// TODO: This event is not supported by Flutter. Add once it's implemented.
28+
static constexpr char kRepeat[] = "repeat";
29+
30+
namespace flutter_desktop_embedding {
31+
KeyEventPlugin::KeyEventPlugin() : JsonPlugin(kChannelName, false) {}
32+
33+
KeyEventPlugin::~KeyEventPlugin() {}
34+
35+
void KeyEventPlugin::CharHook(GLFWwindow *window, unsigned int code_point) {}
36+
37+
void KeyEventPlugin::KeyboardHook(GLFWwindow *window, int key, int scancode,
38+
int action, int mods) {
39+
Json::Value args;
40+
args[kKeyCodeKey] = key;
41+
args[kKeyMapKey] = kAndroidKeyMap;
42+
switch (action) {
43+
case GLFW_PRESS:
44+
args[kTypeKey] = kKeyDown;
45+
break;
46+
case GLFW_RELEASE:
47+
args[kTypeKey] = kKeyUp;
48+
break;
49+
default:
50+
break;
51+
}
52+
/// Messages to flutter/keyevent channels have no method name.
53+
InvokeMethod("", args);
54+
}
55+
56+
void KeyEventPlugin::HandleJsonMethodCall(
57+
const JsonMethodCall &method_call, std::unique_ptr<MethodResult> result) {
58+
// There are no methods invoked from Flutter on this channel.
59+
}
60+
} // namespace flutter_desktop_embedding
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#ifndef LIBRARY_LINUX_SRC_INTERNAL_KEY_EVENT_PLUGIN_H_
15+
#define LIBRARY_LINUX_SRC_INTERNAL_kEY_EVENT_PLUGIN_H_
16+
17+
#include "library/linux/include/flutter_desktop_embedding/json_plugin.h"
18+
#include "library/linux/src/internal/keyboard_hook_handler.h"
19+
20+
namespace flutter_desktop_embedding {
21+
class KeyEventPlugin : public KeyboardHookHandler, public JsonPlugin {
22+
public:
23+
KeyEventPlugin();
24+
virtual ~KeyEventPlugin();
25+
26+
// Plugin.
27+
void HandleJsonMethodCall(const JsonMethodCall &method_call,
28+
std::unique_ptr<MethodResult> result) override;
29+
30+
// KeyboardHookHandler.
31+
void KeyboardHook(GLFWwindow *window, int key, int scancode, int action,
32+
int mods) override;
33+
34+
// KeyboardHookHandler.
35+
void CharHook(GLFWwindow *window, unsigned int code_point) override;
36+
};
37+
} // namespace flutter_desktop_embedding
38+
39+
#endif // LIBRARY_LINUX_SRC_INTERNAL_KEY_EVENT_PLUGIN_H_

library/linux/src/json_method_codec.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ std::unique_ptr<MethodCall> JsonMethodCodec::DecodeMethodCallInternal(
4949
std::unique_ptr<std::vector<uint8_t>> JsonMethodCodec::EncodeMethodCallInternal(
5050
const MethodCall &method_call) const {
5151
Json::Value message(Json::objectValue);
52+
if (method_call.method_name().empty()) {
53+
return EncodeJsonObject(
54+
*static_cast<const Json::Value *>(method_call.arguments()));
55+
}
5256
message[kMessageMethodKey] = method_call.method_name();
5357
message[kMessageArgumentsKey] =
5458
*static_cast<const Json::Value *>(method_call.arguments());

0 commit comments

Comments
 (0)