From 478fc256e42b969c708d42f2c022e951c1cb57c7 Mon Sep 17 00:00:00 2001 From: Azhi <48245812+shixin627@users.noreply.github.com> Date: Mon, 6 Nov 2023 16:32:25 +0800 Subject: [PATCH 1/3] feat: do init in Plugin HandleMethodCall --- .vscode/settings.json | 76 +++++++++++++++++++ example/lib/main.dart | 2 +- .../Flutter/GeneratedPluginRegistrant.swift | 2 + example/pubspec.lock | 63 +++++++++++---- example/pubspec.yaml | 2 +- .../flutter/generated_plugin_registrant.cc | 3 + .../windows/flutter/generated_plugins.cmake | 1 + example/windows/runner/main.cpp | 2 +- lib/src/hid_listener.dart | 11 +++ windows/hid_listener_plugin.cpp | 14 +++- windows/hid_listener_plugin_windows.cpp | 6 +- .../hid_listener_plugin_windows.h | 6 +- 12 files changed, 164 insertions(+), 24 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a35a3b8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,76 @@ +{ + "files.associations": { + "algorithm": "cpp", + "any": "cpp", + "atomic": "cpp", + "bit": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "exception": "cpp", + "format": "cpp", + "forward_list": "cpp", + "functional": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "list": "cpp", + "locale": "cpp", + "map": "cpp", + "memory": "cpp", + "mutex": "cpp", + "new": "cpp", + "optional": "cpp", + "ostream": "cpp", + "ratio": "cpp", + "set": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "string": "cpp", + "system_error": "cpp", + "thread": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "utility": "cpp", + "variant": "cpp", + "vector": "cpp", + "xfacet": "cpp", + "xhash": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocbuf": "cpp", + "xlocinfo": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xstring": "cpp", + "xtr1common": "cpp", + "xtree": "cpp", + "xutility": "cpp" + } +} \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index 538b857..f1e29db 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -14,7 +14,7 @@ void mouseListener(MouseEvent event) { var registerResult = ""; void main() { - if (!getListenerBackend()!.initialize()) { + if (!getListenerBackend()!.doInit()) { print("Failed to initialize listener backend"); } diff --git a/example/macos/Flutter/GeneratedPluginRegistrant.swift b/example/macos/Flutter/GeneratedPluginRegistrant.swift index 121b779..da79353 100644 --- a/example/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/example/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,8 +5,10 @@ import FlutterMacOS import Foundation +import firebase_core import hid_listener func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin")) HidListenerPlugin.register(with: registry.registrar(forPlugin: "HidListenerPlugin")) } diff --git a/example/pubspec.lock b/example/pubspec.lock index a4b1acb..84fba49 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -37,10 +37,10 @@ packages: dependency: transitive description: name: collection - sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c" + sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687 url: "https://pub.dev" source: hosted - version: "1.17.1" + version: "1.17.2" cupertino_icons: dependency: "direct main" description: @@ -65,6 +65,30 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.0" + firebase_core: + dependency: "direct main" + description: + name: firebase_core + sha256: "7706f4ade6cc2698c70074083bc262586a185047f6bfdd53938dcc35d35cbb9e" + url: "https://pub.dev" + source: hosted + version: "2.21.0" + firebase_core_platform_interface: + dependency: transitive + description: + name: firebase_core_platform_interface + sha256: c437ae5d17e6b5cc7981cf6fd458a5db4d12979905f9aafd1fea930428a9fe63 + url: "https://pub.dev" + source: hosted + version: "5.0.0" + firebase_core_web: + dependency: transitive + description: + name: firebase_core_web + sha256: "0631a2ec971dbc540275e2fa00c3a8a2676f0a7adbc3c197d6fba569db689d97" + url: "https://pub.dev" + source: hosted + version: "2.8.1" flutter: dependency: "direct main" description: flutter @@ -83,13 +107,18 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" hid_listener: dependency: "direct main" description: path: ".." relative: true source: path - version: "1.1.3" + version: "2.0.1" js: dependency: transitive description: @@ -110,18 +139,18 @@ packages: dependency: transitive description: name: matcher - sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb" + sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" url: "https://pub.dev" source: hosted - version: "0.12.15" + version: "0.12.16" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724 + sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" url: "https://pub.dev" source: hosted - version: "0.2.0" + version: "0.5.0" meta: dependency: transitive description: @@ -155,10 +184,10 @@ packages: dependency: transitive description: name: source_span - sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250 + sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.10.0" stack_trace: dependency: transitive description: @@ -195,10 +224,10 @@ packages: dependency: transitive description: name: test_api - sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb + sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8" url: "https://pub.dev" source: hosted - version: "0.5.1" + version: "0.6.0" vector_math: dependency: transitive description: @@ -207,6 +236,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" + web: + dependency: transitive + description: + name: web + sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10 + url: "https://pub.dev" + source: hosted + version: "0.1.4-beta" sdks: - dart: ">=3.0.0 <4.0.0" - flutter: ">=2.5.0" + dart: ">=3.1.0-185.0.dev <4.0.0" + flutter: ">=3.3.0" diff --git a/example/pubspec.yaml b/example/pubspec.yaml index ca4ab67..1f324b5 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -25,7 +25,7 @@ dependencies: # The example app is bundled with the plugin so we use a path dependency on # the parent directory to use the current plugin's version. path: ../ - + firebase_core: ^2.21.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 diff --git a/example/windows/flutter/generated_plugin_registrant.cc b/example/windows/flutter/generated_plugin_registrant.cc index 0415cce..f147a42 100644 --- a/example/windows/flutter/generated_plugin_registrant.cc +++ b/example/windows/flutter/generated_plugin_registrant.cc @@ -6,9 +6,12 @@ #include "generated_plugin_registrant.h" +#include #include void RegisterPlugins(flutter::PluginRegistry* registry) { + FirebaseCorePluginCApiRegisterWithRegistrar( + registry->GetRegistrarForPlugin("FirebaseCorePluginCApi")); HidListenerPluginWindowsRegisterWithRegistrar( registry->GetRegistrarForPlugin("HidListenerPluginWindows")); } diff --git a/example/windows/flutter/generated_plugins.cmake b/example/windows/flutter/generated_plugins.cmake index 7027dc2..25d0633 100644 --- a/example/windows/flutter/generated_plugins.cmake +++ b/example/windows/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + firebase_core hid_listener ) diff --git a/example/windows/runner/main.cpp b/example/windows/runner/main.cpp index 6e61c91..a783835 100644 --- a/example/windows/runner/main.cpp +++ b/example/windows/runner/main.cpp @@ -21,7 +21,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev, // plugins. ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); - HidListener listener; + // HidListener listener; flutter::DartProject project(L"data"); diff --git a/lib/src/hid_listener.dart b/lib/src/hid_listener.dart index 7bfa7e0..5d0f458 100644 --- a/lib/src/hid_listener.dart +++ b/lib/src/hid_listener.dart @@ -62,6 +62,17 @@ abstract class HidListenerBackend { mouseListeners.remove(listenerId); } + bool doInit() { + const MethodChannel pluginChannel = MethodChannel("hid_listener"); + pluginChannel.invokeMapMethod("Initialize"); + return initialize(); + } + + void doDispose() { + const MethodChannel pluginChannel = MethodChannel("hid_listener"); + pluginChannel.invokeMapMethod("Dispose"); + } + bool initialize(); bool registerKeyboard(); bool registerMouse(); diff --git a/windows/hid_listener_plugin.cpp b/windows/hid_listener_plugin.cpp index e7141a8..e14c60d 100644 --- a/windows/hid_listener_plugin.cpp +++ b/windows/hid_listener_plugin.cpp @@ -12,6 +12,7 @@ #include #include +#include namespace hid_listener { @@ -36,7 +37,7 @@ void HidListenerPlugin::RegisterWithRegistrar( HidListenerPlugin::HidListenerPlugin() {} HidListenerPlugin::~HidListenerPlugin() {} - +static HidListener listener; void HidListenerPlugin::HandleMethodCall( const flutter::MethodCall &method_call, std::unique_ptr> result) { @@ -51,7 +52,16 @@ void HidListenerPlugin::HandleMethodCall( version_stream << "7"; } result->Success(flutter::EncodableValue(version_stream.str())); - } else { + } + else if (method_call.method_name().compare("Initialize") == 0) { + listener = HidListener(); + printf("HidListenerPlugin::HandleMethodCall: Initialize\n"); + } + else if (method_call.method_name().compare("Dispose") == 0) { + printf("HidListenerPlugin::HandleMethodCall: Dispose\n"); + listener.~HidListener(); + } + else { result->NotImplemented(); } } diff --git a/windows/hid_listener_plugin_windows.cpp b/windows/hid_listener_plugin_windows.cpp index 4d11f4a..ae2e4ba 100644 --- a/windows/hid_listener_plugin_windows.cpp +++ b/windows/hid_listener_plugin_windows.cpp @@ -80,20 +80,20 @@ static LRESULT MouseProc(int nCode, WPARAM wParam, LPARAM lParam) { #if defined(__cplusplus) -HidListener* HidListener::listenerInstance = nullptr; +HidListener* HidListener::ListenerInstance = nullptr; HidListener::HidListener() { m_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, NULL); m_mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, NULL, NULL); - listenerInstance = this; + ListenerInstance = this; } HidListener::~HidListener() { UnhookWindowsHookEx(m_keyboardHook); UnhookWindowsHookEx(m_mouseHook); - listenerInstance = nullptr; + ListenerInstance = nullptr; } #endif diff --git a/windows/include/hid_listener/hid_listener_plugin_windows.h b/windows/include/hid_listener/hid_listener_plugin_windows.h index e88ff50..dc16713 100644 --- a/windows/include/hid_listener/hid_listener_plugin_windows.h +++ b/windows/include/hid_listener/hid_listener_plugin_windows.h @@ -11,19 +11,19 @@ extern "C" #if defined(__cplusplus) #include - class FLUTTER_PLUGIN_EXPORT HidListener + class HidListener { public: HidListener(); ~HidListener(); - static HidListener* Get() { return HidListener::listenerInstance; } + static HidListener* Get() { return HidListener::ListenerInstance; } private: HHOOK m_keyboardHook; HHOOK m_mouseHook; - static HidListener* listenerInstance; + static HidListener* ListenerInstance; }; #endif From d0d2d0b786dc90aeb3721916108a9d9923be7208 Mon Sep 17 00:00:00 2001 From: Azhi <48245812+shixin627@users.noreply.github.com> Date: Mon, 6 Nov 2023 21:41:48 +0800 Subject: [PATCH 2/3] feat: declare HidListener in plugin class --- example/lib/main.dart | 13 +++++++------ lib/src/hid_listener.dart | 4 ++-- windows/hid_listener_plugin.cpp | 4 +--- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index f1e29db..f99ae60 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -14,13 +14,14 @@ void mouseListener(MouseEvent event) { var registerResult = ""; void main() { - if (!getListenerBackend()!.doInit()) { - print("Failed to initialize listener backend"); + bool initialized = getListenerBackend()!.doInit(); + if (!initialized) { + registerResult = "Failed to initialize listener backend"; + } else { + getListenerBackend()!.addKeyboardListener(listener); + getListenerBackend()!.addMouseListener(mouseListener); + registerResult = "Initialized listener backend"; } - - getListenerBackend()!.addKeyboardListener(listener); - getListenerBackend()!.addMouseListener(mouseListener); - runApp(const MyApp()); } diff --git a/lib/src/hid_listener.dart b/lib/src/hid_listener.dart index 5d0f458..c7a5396 100644 --- a/lib/src/hid_listener.dart +++ b/lib/src/hid_listener.dart @@ -64,13 +64,13 @@ abstract class HidListenerBackend { bool doInit() { const MethodChannel pluginChannel = MethodChannel("hid_listener"); - pluginChannel.invokeMapMethod("Initialize"); + pluginChannel.invokeMethod("Initialize"); return initialize(); } void doDispose() { const MethodChannel pluginChannel = MethodChannel("hid_listener"); - pluginChannel.invokeMapMethod("Dispose"); + pluginChannel.invokeMethod("Dispose"); } bool initialize(); diff --git a/windows/hid_listener_plugin.cpp b/windows/hid_listener_plugin.cpp index e14c60d..c53f237 100644 --- a/windows/hid_listener_plugin.cpp +++ b/windows/hid_listener_plugin.cpp @@ -15,7 +15,7 @@ #include namespace hid_listener { - +HidListener listener; // static void HidListenerPlugin::RegisterWithRegistrar( flutter::PluginRegistrarWindows *registrar) { @@ -37,7 +37,6 @@ void HidListenerPlugin::RegisterWithRegistrar( HidListenerPlugin::HidListenerPlugin() {} HidListenerPlugin::~HidListenerPlugin() {} -static HidListener listener; void HidListenerPlugin::HandleMethodCall( const flutter::MethodCall &method_call, std::unique_ptr> result) { @@ -54,7 +53,6 @@ void HidListenerPlugin::HandleMethodCall( result->Success(flutter::EncodableValue(version_stream.str())); } else if (method_call.method_name().compare("Initialize") == 0) { - listener = HidListener(); printf("HidListenerPlugin::HandleMethodCall: Initialize\n"); } else if (method_call.method_name().compare("Dispose") == 0) { From 3f6bcf16584b915db0113f86c48938e220db9c71 Mon Sep 17 00:00:00 2001 From: Azhi <48245812+shixin627@users.noreply.github.com> Date: Tue, 7 Nov 2023 11:23:33 +0800 Subject: [PATCH 3/3] feat: move ListenerInstance to global from Listener --- example/windows/runner/main.cpp | 2 +- windows/hid_listener_plugin.cpp | 3 --- windows/hid_listener_plugin_windows.cpp | 7 +++---- windows/include/hid_listener/hid_listener_plugin_windows.h | 7 +------ 4 files changed, 5 insertions(+), 14 deletions(-) diff --git a/example/windows/runner/main.cpp b/example/windows/runner/main.cpp index a783835..6e61c91 100644 --- a/example/windows/runner/main.cpp +++ b/example/windows/runner/main.cpp @@ -21,7 +21,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev, // plugins. ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); - // HidListener listener; + HidListener listener; flutter::DartProject project(L"data"); diff --git a/windows/hid_listener_plugin.cpp b/windows/hid_listener_plugin.cpp index c53f237..5d2db1d 100644 --- a/windows/hid_listener_plugin.cpp +++ b/windows/hid_listener_plugin.cpp @@ -12,10 +12,8 @@ #include #include -#include namespace hid_listener { -HidListener listener; // static void HidListenerPlugin::RegisterWithRegistrar( flutter::PluginRegistrarWindows *registrar) { @@ -57,7 +55,6 @@ void HidListenerPlugin::HandleMethodCall( } else if (method_call.method_name().compare("Dispose") == 0) { printf("HidListenerPlugin::HandleMethodCall: Dispose\n"); - listener.~HidListener(); } else { result->NotImplemented(); diff --git a/windows/hid_listener_plugin_windows.cpp b/windows/hid_listener_plugin_windows.cpp index ae2e4ba..7bd8cea 100644 --- a/windows/hid_listener_plugin_windows.cpp +++ b/windows/hid_listener_plugin_windows.cpp @@ -79,8 +79,7 @@ static LRESULT MouseProc(int nCode, WPARAM wParam, LPARAM lParam) { } #if defined(__cplusplus) - -HidListener* HidListener::ListenerInstance = nullptr; +HidListener* ListenerInstance = nullptr; HidListener::HidListener() { m_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, NULL); @@ -99,13 +98,13 @@ HidListener::~HidListener() { #endif bool SetKeyboardListener(Dart_Port port) { - if(HidListener::Get() == nullptr) return false; + if(ListenerInstance == nullptr) return false; keyboardListenerPort = port; return true; } bool SetMouseListener(Dart_Port port) { - if(HidListener::Get() == nullptr) return false; + if(ListenerInstance == nullptr) return false; mouseListenerPort = port; return true; } diff --git a/windows/include/hid_listener/hid_listener_plugin_windows.h b/windows/include/hid_listener/hid_listener_plugin_windows.h index dc16713..e5caa91 100644 --- a/windows/include/hid_listener/hid_listener_plugin_windows.h +++ b/windows/include/hid_listener/hid_listener_plugin_windows.h @@ -11,19 +11,14 @@ extern "C" #if defined(__cplusplus) #include - class HidListener + class FLUTTER_PLUGIN_EXPORT HidListener { public: HidListener(); ~HidListener(); - - static HidListener* Get() { return HidListener::ListenerInstance; } - private: HHOOK m_keyboardHook; HHOOK m_mouseHook; - - static HidListener* ListenerInstance; }; #endif