Skip to content

Commit 9dc42ad

Browse files
[linux] Adds raw key events plugin.
1 parent 4990b13 commit 9dc42ad

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"
@@ -276,13 +277,17 @@ GLFWwindow *CreateFlutterWindow(size_t initial_width, size_t initial_height,
276277
FlutterEmbedderState *state = new FlutterEmbedderState();
277278
state->plugin_handler = std::make_unique<PluginHandler>(engine);
278279
state->engine = engine;
280+
281+
auto key_event_plugin = std::make_unique<KeyEventPlugin>();
282+
state->keyboard_hook_handlers.push_back(key_event_plugin.get());
279283
auto input_plugin = std::make_unique<TextInputPlugin>();
280284
state->keyboard_hook_handlers.push_back(input_plugin.get());
281-
285+
282286
glfwSetWindowUserPointer(window, state);
283287

288+
AddPlugin(window, std::move(key_event_plugin));
284289
AddPlugin(window, std::move(input_plugin));
285-
290+
286291
int width, height;
287292
glfwGetWindowSize(window, &width, &height);
288293
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
@@ -58,6 +58,10 @@ std::unique_ptr<MethodCall> JsonMethodCodec::DecodeMethodCallInternal(
5858
std::unique_ptr<std::vector<uint8_t>> JsonMethodCodec::EncodeMethodCallInternal(
5959
const MethodCall &method_call) const {
6060
Json::Value message(Json::objectValue);
61+
if (method_call.method_name().empty()) {
62+
return EncodeJsonObject(
63+
*static_cast<const Json::Value *>(method_call.arguments()));
64+
}
6165
message[kMessageMethodKey] = method_call.method_name();
6266
message[kMessageArgumentsKey] =
6367
*static_cast<const Json::Value *>(method_call.arguments());

0 commit comments

Comments
 (0)