From 843318da5a90966336337e0849a1cbe9dc9c1f2e Mon Sep 17 00:00:00 2001
From: JunsuChoi <jsuya.choi@samsung.com>
Date: Tue, 2 Apr 2024 18:34:43 +0900
Subject: [PATCH] Support device type for KeyEvent

---
 .../tizen/channels/input_device_channel.h      |  2 ++
 .../tizen/channels/keyboard_channel.cc         |  8 ++++++--
 .../platform/tizen/channels/keyboard_channel.h |  4 +++-
 .../shell/platform/tizen/flutter_tizen_view.cc | 18 +++++++++++++++++-
 4 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/flutter/shell/platform/tizen/channels/input_device_channel.h b/flutter/shell/platform/tizen/channels/input_device_channel.h
index 3f91080..a036ce2 100644
--- a/flutter/shell/platform/tizen/channels/input_device_channel.h
+++ b/flutter/shell/platform/tizen/channels/input_device_channel.h
@@ -23,6 +23,8 @@ class InputDeviceChannel {
     last_keyboard_name_ = name;
   }
 
+  std::string last_keyboard_name() { return last_keyboard_name_; }
+
  private:
   void HandleMethodCall(const MethodCall<EncodableValue>& method_call,
                         std::unique_ptr<MethodResult<EncodableValue>> result);
diff --git a/flutter/shell/platform/tizen/channels/keyboard_channel.cc b/flutter/shell/platform/tizen/channels/keyboard_channel.cc
index 1d10573..ec107ae 100644
--- a/flutter/shell/platform/tizen/channels/keyboard_channel.cc
+++ b/flutter/shell/platform/tizen/channels/keyboard_channel.cc
@@ -118,6 +118,7 @@ void KeyboardChannel::SendKey(const char* key,
                               uint32_t modifiers,
                               uint32_t scan_code,
                               bool is_down,
+                              FlutterKeyEventDeviceType device_type,
                               std::function<void(bool)> callback) {
   uint64_t sequence_id = last_sequence_id_++;
 
@@ -142,7 +143,7 @@ void KeyboardChannel::SendKey(const char* key,
   }
 
   SendEmbedderEvent(key, string, compose, modifiers, scan_code, is_down,
-                    sequence_id);
+                    sequence_id, device_type);
   // The channel-based API (RawKeyEvent) is deprecated and |SendChannelEvent|
   // will be removed in the future. This class (KeyboardChannel) itself will
   // also be renamed and refactored then.
@@ -205,7 +206,8 @@ void KeyboardChannel::SendEmbedderEvent(const char* key,
                                         uint32_t modifiers,
                                         uint32_t scan_code,
                                         bool is_down,
-                                        uint64_t sequence_id) {
+                                        uint64_t sequence_id,
+                                        FlutterKeyEventDeviceType device_type) {
   uint64_t physical_key = GetPhysicalKey(scan_code);
   uint64_t logical_key = GetLogicalKey(key);
   const char* character = is_down ? string : nullptr;
@@ -242,6 +244,7 @@ void KeyboardChannel::SendEmbedderEvent(const char* key,
           .logical = 0,
           .character = "",
           .synthesized = false,
+          .device_type = device_type,
       };
       send_event_(empty_event, nullptr, nullptr);
       ResolvePendingEvent(sequence_id, true);
@@ -263,6 +266,7 @@ void KeyboardChannel::SendEmbedderEvent(const char* key,
   event.logical = last_logical_record != 0 ? last_logical_record : logical_key;
   event.character = character;
   event.synthesized = false;
+  event.device_type = device_type;
 
   send_event_(
       event,
diff --git a/flutter/shell/platform/tizen/channels/keyboard_channel.h b/flutter/shell/platform/tizen/channels/keyboard_channel.h
index 8127d24..34dfba9 100644
--- a/flutter/shell/platform/tizen/channels/keyboard_channel.h
+++ b/flutter/shell/platform/tizen/channels/keyboard_channel.h
@@ -35,6 +35,7 @@ class KeyboardChannel {
                uint32_t modifiers,
                uint32_t scan_code,
                bool is_down,
+               FlutterKeyEventDeviceType device_type,
                std::function<void(bool)> callback);
 
  private:
@@ -69,7 +70,8 @@ class KeyboardChannel {
                          uint32_t modifiers,
                          uint32_t scan_code,
                          bool is_down,
-                         uint64_t sequence_id);
+                         uint64_t sequence_id,
+                         FlutterKeyEventDeviceType device_type);
 
   void SendChannelEvent(const char* key,
                         const char* string,
diff --git a/flutter/shell/platform/tizen/flutter_tizen_view.cc b/flutter/shell/platform/tizen/flutter_tizen_view.cc
index 5074790..5c59de0 100644
--- a/flutter/shell/platform/tizen/flutter_tizen_view.cc
+++ b/flutter/shell/platform/tizen/flutter_tizen_view.cc
@@ -45,6 +45,11 @@ const std::vector<std::string> kBindableSystemKeys = {
     "XF86Exit",
 };
 
+const std::vector<std::string> kRemoteControlDeviceTypeNameKeywords = {
+    "rc device",      // wt61p807 rc device
+    "Smart Control",  // Smart Control 2016
+};
+
 // The multiplier is taken from the Chromium source
 // (ui/events/x/events_x_utils.cc).
 constexpr int32_t kScrollOffsetMultiplier = 53;
@@ -352,9 +357,20 @@ void FlutterTizenView::OnKey(const char* key,
     }
   }
 
+  FlutterKeyEventDeviceType device_type =
+      FlutterKeyEventDeviceType::kFlutterKeyEventDeviceTypeKeyboard;
+  for (const std::string& key : kRemoteControlDeviceTypeNameKeywords) {
+    if (input_device_channel_->last_keyboard_name().find(key) !=
+        std::string::npos) {
+      device_type =
+          FlutterKeyEventDeviceType::kFlutterKeyEventDeviceTypeDirectionalPad;
+      break;
+    }
+  }
+
   if (engine_->keyboard_channel()) {
     engine_->keyboard_channel()->SendKey(
-        key, string, compose, modifiers, scan_code, is_down,
+        key, string, compose, modifiers, scan_code, is_down, device_type,
         [engine = engine_.get(), symbol = std::string(key),
          is_down](bool handled) {
           if (handled) {