5#include "flutter/shell/platform/windows/keyboard_key_handler.h"
9#include "flutter/fml/logging.h"
10#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_method_codec.h"
11#include "flutter/shell/platform/windows/keyboard_utils.h"
19static constexpr int kMaxPendingEvents = 1000;
22static constexpr char kChannelName[] =
"flutter/keyboard";
32 : last_sequence_id_(1),
41 channel_->SetMethodCallHandler(
48void KeyboardKeyHandler::HandleMethodCall(
51 const std::string& method =
method_call.method_name();
55 for (
const auto& pressed_key : pressed_state) {
56 EncodableValue physical_value(
static_cast<long long>(pressed_key.first));
57 EncodableValue logical_value(
static_cast<long long>(pressed_key.second));
58 value[physical_value] = logical_value;
67 std::unique_ptr<KeyboardKeyHandlerDelegate> delegate) {
68 delegates_.push_back(std::move(delegate));
73 auto& key_embedder_handler = delegates_.front();
74 key_embedder_handler->SyncModifiersIfNeeded(modifiers_state);
79 auto& key_embedder_handler = delegates_.front();
80 return key_embedder_handler->GetPressedState();
90 std::unique_ptr<PendingEvent> incoming = std::make_unique<PendingEvent>();
92 uint64_t sequence_id = ++last_sequence_id_;
93 incoming->sequence_id = sequence_id;
94 incoming->unreplied = delegates_.size();
95 incoming->any_handled =
false;
96 incoming->callback = std::move(
callback);
98 if (pending_responds_.size() > kMaxPendingEvents) {
100 <<
"There are " << pending_responds_.size()
101 <<
" keyboard events that have not yet received a response from the "
102 <<
"framework. Are responses being sent?";
104 pending_responds_.push_back(std::move(incoming));
106 for (
const auto& delegate : delegates_) {
108 [sequence_id,
this](
bool handled) {
109 ResolvePendingEvent(sequence_id, handled);
120void KeyboardKeyHandler::ResolvePendingEvent(uint64_t sequence_id,
123 for (
auto iter = pending_responds_.begin(); iter != pending_responds_.end();
125 if ((*iter)->sequence_id == sequence_id) {
126 PendingEvent&
event = **iter;
127 event.any_handled =
event.any_handled || handled;
128 event.unreplied -= 1;
130 <<
"Pending events must have unreplied count > 0";
132 if (
event.unreplied == 0) {
133 std::unique_ptr<PendingEvent> event_ptr = std::move(*iter);
134 pending_responds_.erase(iter);
135 event.callback(
event.any_handled);
142 FML_LOG(
FATAL) <<
"Could not find pending key event for sequence ID "
std::function< void(bool)> KeyEventCallback
virtual ~KeyboardKeyHandlerDelegate()
void SyncModifiersIfNeeded(int modifiers_state) override
KeyboardKeyHandler(flutter::BinaryMessenger *messenger)
void KeyboardHook(int key, int scancode, int action, char32_t character, bool extended, bool was_down, KeyEventCallback callback) override
void AddDelegate(std::unique_ptr< KeyboardKeyHandlerDelegate > delegate)
std::map< uint64_t, uint64_t > GetPressedState() override
void InitKeyboardChannel()
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
static constexpr char kGetKeyboardStateMethod[]
G_BEGIN_DECLS G_MODULE_EXPORT FlMethodCall * method_call
#define FML_LOG(severity)
#define FML_DCHECK(condition)
std::map< EncodableValue, EncodableValue > EncodableMap