Skip to content

Commit 1888375

Browse files
[linux] Adds raw key event handler (#163)
1 parent 37919be commit 1888375

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

library/linux/src/embedder.cc

Lines changed: 9 additions & 0 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_handler.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"
@@ -50,6 +51,10 @@ struct FlutterEmbedderState {
5051
// deleted from the heap.
5152
std::vector<flutter_desktop_embedding::KeyboardHookHandler *>
5253
keyboard_hook_handlers;
54+
// Handles raw key interactions from GLFW.
55+
// TODO: Move key_event_handler once
56+
// https://github.com/google/flutter-desktop-embedding/issues/102 is resolved.
57+
std::unique_ptr<flutter_desktop_embedding::KeyEventHandler> key_event_handler;
5358
};
5459

5560
static constexpr char kDefaultWindowTitle[] = "Flutter";
@@ -272,6 +277,10 @@ GLFWwindow *CreateFlutterWindow(size_t initial_width, size_t initial_height,
272277
FlutterEmbedderState *state = new FlutterEmbedderState();
273278
state->plugin_handler = std::make_unique<PluginHandler>(engine);
274279
state->engine = engine;
280+
281+
state->key_event_handler =
282+
std::make_unique<KeyEventHandler>(state->plugin_handler.get());
283+
state->keyboard_hook_handlers.push_back(state->key_event_handler.get());
275284
auto input_plugin = std::make_unique<TextInputPlugin>();
276285
state->keyboard_hook_handlers.push_back(input_plugin.get());
277286

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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_handler.h"
15+
16+
#include <json/json.h>
17+
#include <iostream>
18+
19+
#include "library/linux/src/internal/json_message_codec.h"
20+
21+
static constexpr char kChannelName[] = "flutter/keyevent";
22+
23+
static constexpr char kKeyCodeKey[] = "keyCode";
24+
static constexpr char kKeyMapKey[] = "keymap";
25+
static constexpr char kTypeKey[] = "type";
26+
27+
static constexpr char kAndroidKeyMap[] = "android";
28+
static constexpr char kKeyUp[] = "keyup";
29+
static constexpr char kKeyDown[] = "keydown";
30+
// TODO: This event is not supported by Flutter. Add once it's implemented.
31+
static constexpr char kRepeat[] = "repeat";
32+
33+
namespace flutter_desktop_embedding {
34+
35+
KeyEventHandler::KeyEventHandler(const BinaryMessenger *messenger)
36+
: messenger_(messenger), channel_(kChannelName) {}
37+
38+
KeyEventHandler::~KeyEventHandler() {}
39+
40+
void KeyEventHandler::CharHook(GLFWwindow *window, unsigned int code_point) {}
41+
42+
void KeyEventHandler::KeyboardHook(GLFWwindow *window, int key, int scancode,
43+
int action, int mods) {
44+
// TODO: Translate to a cross-platform key code system rather than passing
45+
// the native key code.
46+
Json::Value args;
47+
args[kKeyCodeKey] = key;
48+
args[kKeyMapKey] = kAndroidKeyMap;
49+
50+
switch (action) {
51+
case GLFW_PRESS:
52+
args[kTypeKey] = kKeyDown;
53+
break;
54+
case GLFW_RELEASE:
55+
args[kTypeKey] = kKeyUp;
56+
break;
57+
default:
58+
std::cerr << "Unknown key event action: " << action << std::endl;
59+
return;
60+
}
61+
auto message = JsonMessageCodec::GetInstance().EncodeMessage(args);
62+
messenger_->Send(channel_, message->data(), message->size());
63+
}
64+
65+
} // namespace flutter_desktop_embedding
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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_HANDLER_H_
15+
#define LIBRARY_LINUX_SRC_INTERNAL_kEY_EVENT_HANDLER_H_
16+
17+
#include "library/linux/include/flutter_desktop_embedding/binary_messenger.h"
18+
#include "library/linux/src/internal/keyboard_hook_handler.h"
19+
20+
namespace flutter_desktop_embedding {
21+
22+
// Implements a KeyboardHookHandler
23+
//
24+
// Handles key events and forwards them to the Flutter engine.
25+
class KeyEventHandler : public KeyboardHookHandler {
26+
public:
27+
explicit KeyEventHandler(const BinaryMessenger *messenger);
28+
virtual ~KeyEventHandler();
29+
30+
// KeyboardHookHandler.
31+
void KeyboardHook(GLFWwindow *window, int key, int scancode, int action,
32+
int mods) override;
33+
void CharHook(GLFWwindow *window, unsigned int code_point) override;
34+
35+
private:
36+
// Binds this plugin to the given caller-owned binary messenger. It must
37+
// remain valid for the life of the plugin.
38+
const BinaryMessenger *messenger_;
39+
std::string channel_;
40+
};
41+
42+
} // namespace flutter_desktop_embedding
43+
44+
#endif // LIBRARY_LINUX_SRC_INTERNAL_KEY_EVENT_HANDLER_H_

0 commit comments

Comments
 (0)