Skip to content

Commit

Permalink
Integrate keyboard channel (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
JSUYA authored Nov 29, 2023
1 parent acf40d2 commit 6f94b9c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 29 deletions.
2 changes: 1 addition & 1 deletion flutter/shell/platform/tizen/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ template("embedder") {
"channels/app_control_channel.cc",
"channels/feedback_manager.cc",
"channels/input_device_channel.cc",
"channels/key_event_channel.cc",
"channels/key_mapping.cc",
"channels/keyboard_channel.cc",
"channels/lifecycle_channel.cc",
"channels/mouse_cursor_channel.cc",
"channels/navigation_channel.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "key_event_channel.h"
#include "keyboard_channel.h"

#include <chrono>
#include <codecvt>
#include <locale>
#include <string>

#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_method_codec.h"
#include "flutter/shell/platform/common/json_message_codec.h"
#include "flutter/shell/platform/tizen/channels/key_mapping.h"
#include "flutter/shell/platform/tizen/logger.h"
Expand All @@ -17,8 +18,10 @@ namespace flutter {

namespace {

constexpr char kChannelName[] = "flutter/keyevent";
constexpr char kKeyboardChannelName[] = "flutter/keyboard";
constexpr char kKeyEventChannelName[] = "flutter/keyevent";

constexpr char kGetKeyboardStateMethod[] = "getKeyboardState";
constexpr char kKeyMapKey[] = "keymap";
constexpr char kKeyCodeKey[] = "keyCode";
constexpr char kScanCodeKey[] = "scanCode";
Expand Down Expand Up @@ -88,17 +91,28 @@ uint32_t GetFallbackScanCodeFromKey(const std::string& key) {

} // namespace

KeyEventChannel::KeyEventChannel(BinaryMessenger* messenger,
KeyboardChannel::KeyboardChannel(BinaryMessenger* messenger,
SendEventHandler send_event)
: channel_(std::make_unique<BasicMessageChannel<rapidjson::Document>>(
: keyboard_channel_(std::make_unique<MethodChannel<EncodableValue>>(
messenger,
kChannelName,
&JsonMessageCodec::GetInstance())),
send_event_(send_event) {}
kKeyboardChannelName,
&StandardMethodCodec::GetInstance())),
key_event_channel_(
std::make_unique<BasicMessageChannel<rapidjson::Document>>(
messenger,
kKeyEventChannelName,
&JsonMessageCodec::GetInstance())),
send_event_(send_event) {
keyboard_channel_->SetMethodCallHandler(
[this](const MethodCall<EncodableValue>& call,
std::unique_ptr<MethodResult<EncodableValue>> result) {
HandleMethodCall(call, std::move(result));
});
}

KeyEventChannel::~KeyEventChannel() {}
KeyboardChannel::~KeyboardChannel() {}

void KeyEventChannel::SendKey(const char* key,
void KeyboardChannel::SendKey(const char* key,
const char* string,
const char* compose,
uint32_t modifiers,
Expand Down Expand Up @@ -130,13 +144,13 @@ void KeyEventChannel::SendKey(const char* key,
SendEmbedderEvent(key, string, compose, modifiers, scan_code, is_down,
sequence_id);
// The channel-based API (RawKeyEvent) is deprecated and |SendChannelEvent|
// will be removed in the future. This class (KeyEventChannel) itself will
// will be removed in the future. This class (KeyboardChannel) itself will
// also be renamed and refactored then.
SendChannelEvent(key, string, compose, modifiers, scan_code, is_down,
sequence_id);
}

void KeyEventChannel::SendChannelEvent(const char* key,
void KeyboardChannel::SendChannelEvent(const char* key,
const char* string,
const char* compose,
uint32_t modifiers,
Expand Down Expand Up @@ -174,7 +188,7 @@ void KeyEventChannel::SendChannelEvent(const char* key,
} else {
event.AddMember(kTypeKey, kKeyUp, allocator);
}
channel_->Send(
key_event_channel_->Send(
event, [this, sequence_id](const uint8_t* reply, size_t reply_size) {
if (reply != nullptr) {
std::unique_ptr<rapidjson::Document> decoded =
Expand All @@ -185,7 +199,7 @@ void KeyEventChannel::SendChannelEvent(const char* key,
});
}

void KeyEventChannel::SendEmbedderEvent(const char* key,
void KeyboardChannel::SendEmbedderEvent(const char* key,
const char* string,
const char* compose,
uint32_t modifiers,
Expand Down Expand Up @@ -262,7 +276,7 @@ void KeyEventChannel::SendEmbedderEvent(const char* key,
}));
}

void KeyEventChannel::ResolvePendingEvent(uint64_t sequence_id, bool handled) {
void KeyboardChannel::ResolvePendingEvent(uint64_t sequence_id, bool handled) {
auto iter = pending_events_.find(sequence_id);
if (iter != pending_events_.end()) {
PendingEvent* event = iter->second.get();
Expand All @@ -279,4 +293,22 @@ void KeyEventChannel::ResolvePendingEvent(uint64_t sequence_id, bool handled) {
FT_ASSERT_NOT_REACHED();
}

void KeyboardChannel::HandleMethodCall(
const MethodCall<EncodableValue>& method_call,
std::unique_ptr<MethodResult<EncodableValue>> result) {
const std::string& method_name = method_call.method_name();
if (method_name == kGetKeyboardStateMethod) {
EncodableMap map;
for (const auto& key : pressing_records_) {
EncodableValue physical_value(static_cast<int64_t>(key.first));
EncodableValue logical_value(static_cast<int64_t>(key.second));
map[physical_value] = logical_value;
}

result->Success(EncodableValue(map));
} else {
result->NotImplemented();
}
}

} // namespace flutter
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EMBEDDER_KEY_EVENT_CHANNEL_H_
#define EMBEDDER_KEY_EVENT_CHANNEL_H_
#ifndef EMBEDDER_KEYBOARD_CHANNEL_H_
#define EMBEDDER_KEYBOARD_CHANNEL_H_

#include <functional>
#include <map>
#include <memory>

#include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h"
#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h"
#include "flutter/shell/platform/common/client_wrapper/include/flutter/encodable_value.h"
#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h"
#include "flutter/shell/platform/embedder/embedder.h"

#include "rapidjson/document.h"

namespace flutter {

class KeyEventChannel {
class KeyboardChannel {
public:
using SendEventHandler = std::function<void(const FlutterKeyEvent& event,
FlutterKeyEventCallback callback,
void* user_data)>;

explicit KeyEventChannel(BinaryMessenger* messenger,
explicit KeyboardChannel(BinaryMessenger* messenger,
SendEventHandler send_event);
virtual ~KeyEventChannel();
virtual ~KeyboardChannel();

void SendKey(const char* key,
const char* string,
Expand All @@ -35,7 +38,8 @@ class KeyEventChannel {
std::function<void(bool)> callback);

private:
std::unique_ptr<BasicMessageChannel<rapidjson::Document>> channel_;
std::unique_ptr<flutter::MethodChannel<EncodableValue>> keyboard_channel_;
std::unique_ptr<BasicMessageChannel<rapidjson::Document>> key_event_channel_;
SendEventHandler send_event_;

struct PendingEvent {
Expand Down Expand Up @@ -76,8 +80,12 @@ class KeyEventChannel {
uint64_t sequence_id);

void ResolvePendingEvent(uint64_t sequence_id, bool handled);

void HandleMethodCall(
const flutter::MethodCall<EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<EncodableValue>> result);
};

} // namespace flutter

#endif // EMBEDDER_KEY_EVENT_CHANNEL_H_
#endif // EMBEDDER_KEYBOARD_CHANNEL_H_
2 changes: 1 addition & 1 deletion flutter/shell/platform/tizen/flutter_tizen_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ bool FlutterTizenEngine::RunEngine() {

if (IsHeaded()) {
texture_registrar_ = std::make_unique<FlutterTizenTextureRegistrar>(this);
key_event_channel_ = std::make_unique<KeyEventChannel>(
keyboard_channel_ = std::make_unique<KeyboardChannel>(
internal_plugin_registrar_->messenger(),
[this](const FlutterKeyEvent& event, FlutterKeyEventCallback callback,
void* user_data) { SendKeyEvent(event, callback, user_data); });
Expand Down
8 changes: 4 additions & 4 deletions flutter/shell/platform/tizen/flutter_tizen_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "flutter/shell/platform/tizen/accessibility_settings.h"
#include "flutter/shell/platform/tizen/channels/accessibility_channel.h"
#include "flutter/shell/platform/tizen/channels/app_control_channel.h"
#include "flutter/shell/platform/tizen/channels/key_event_channel.h"
#include "flutter/shell/platform/tizen/channels/keyboard_channel.h"
#include "flutter/shell/platform/tizen/channels/lifecycle_channel.h"
#include "flutter/shell/platform/tizen/channels/navigation_channel.h"
#include "flutter/shell/platform/tizen/channels/platform_view_channel.h"
Expand Down Expand Up @@ -101,7 +101,7 @@ class FlutterTizenEngine {
return app_control_channel_.get();
}

KeyEventChannel* key_event_channel() { return key_event_channel_.get(); }
KeyboardChannel* keyboard_channel() { return keyboard_channel_.get(); }

LifecycleChannel* lifecycle_channel() { return lifecycle_channel_.get(); }

Expand Down Expand Up @@ -251,8 +251,8 @@ class FlutterTizenEngine {
// A plugin that implements the Tizen app_control channel.
std::unique_ptr<AppControlChannel> app_control_channel_;

// A plugin that implements the Flutter keyevent channel.
std::unique_ptr<KeyEventChannel> key_event_channel_;
// A plugin that implements the Flutter keyboard channel.
std::unique_ptr<KeyboardChannel> keyboard_channel_;

// A plugin that implements the Flutter lifecycle channel.
std::unique_ptr<LifecycleChannel> lifecycle_channel_;
Expand Down
4 changes: 2 additions & 2 deletions flutter/shell/platform/tizen/flutter_tizen_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ void FlutterTizenView::OnKey(const char* key,
}
}

if (engine_->key_event_channel()) {
engine_->key_event_channel()->SendKey(
if (engine_->keyboard_channel()) {
engine_->keyboard_channel()->SendKey(
key, string, compose, modifiers, scan_code, is_down,
[engine = engine_.get(), symbol = std::string(key),
is_down](bool handled) {
Expand Down

0 comments on commit 6f94b9c

Please sign in to comment.