Skip to content

Commit 7fdb8ca

Browse files
author
StArray
committed
fix(input): 优化输入事件钩子的安装和处理逻辑
- 将输入钩子安装方法由 InstallHooks 改名为 InstallInputHooks - 新增对 libinput.so 中多个消费输入事件函数的钩子支持 - 修改触摸事件钩子回调为 OnConsumeSamples,增强事件处理 - 根据是否初始化,通过事件指针对 ImGuiImplAndroid 进行输入事件处理 - 统一更新 VulkanRenderer 和 EGLRenderer 中的输入钩子安装调用代码
1 parent 8205c8f commit 7fdb8ca

5 files changed

Lines changed: 46 additions & 44 deletions

File tree

‎.github/workflows/build.yml‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,41 @@ jobs:
4343
name: library-release.aar
4444
path: Android/library/build/outputs/aar/library-release.aar
4545

46-
# ──── Windows Native + .NET DLL (cross-compiled on Linux) ────
46+
# ──── Windows Native + .NET DLL ────
4747
windows:
4848
name: Windows
49-
runs-on: ubuntu-latest
49+
runs-on: windows-latest
50+
defaults:
51+
run:
52+
shell: bash
5053
steps:
5154
- uses: actions/checkout@v4
5255
with:
5356
submodules: recursive
5457

55-
- name: Install MinGW cross-compiler
56-
run: |
57-
sudo apt-get update
58-
sudo apt-get install -y \
59-
gcc-mingw-w64-x86-64 \
60-
g++-mingw-w64-x86-64 \
61-
mingw-w64-tools \
62-
cmake \
63-
ninja-build
58+
- name: Set up MSYS2 (MinGW)
59+
uses: msys2/setup-msys2@v2
60+
with:
61+
msystem: mingw64
62+
update: true
63+
install: >-
64+
mingw-w64-x86_64-gcc
65+
mingw-w64-x86_64-cmake
66+
mingw-w64-x86_64-toolchain
6467
6568
- name: Set up .NET 10
6669
uses: actions/setup-dotnet@v4
6770
with:
6871
dotnet-version: '10.0.x'
6972

70-
- name: Build Native (CMake + MinGW cross)
73+
- name: Build Native (CMake + MinGW)
74+
shell: msys2 {0}
7175
run: |
7276
cd StArray.ModManager.Windows/Native
73-
# Generate cimgui import lib from pre-built DLL
74-
x86_64-w64-mingw32-gendef - ../dlls/cimgui.dll > cimgui.def
75-
x86_64-w64-mingw32-dlltool -d cimgui.def -l libcimgui.a -D ../dlls/cimgui.dll
76-
cmake -B build -G Ninja \
77-
-DCMAKE_BUILD_TYPE=Release \
78-
-DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc \
79-
-DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ \
80-
-DCMAKE_RC_COMPILER=x86_64-w64-mingw32-windres
77+
# Generate cimgui import lib from DLL
78+
gendef - ../dlls/cimgui.dll > cimgui.def
79+
dlltool -d cimgui.def -l libcimgui.a -D ../dlls/cimgui.dll
80+
cmake -B build -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release
8181
cmake --build build
8282
8383
- name: Build C#
@@ -90,13 +90,13 @@ jobs:
9090
mkdir -p "$dst"
9191
cp -r "$src"/* "$dst/"
9292
rm -rf "$dst/runtimes"
93-
cd "$dst" && zip -r ../windows-dll.zip . && cd ..
93+
tar -czf windows-dll.tar.gz -C "$dst" .
9494
9595
- name: Upload
9696
uses: actions/upload-artifact@v4
9797
with:
9898
name: windows-dll
99-
path: windows-dll.zip
99+
path: windows-dll.tar.gz
100100

101101
# ──── Android .NET DLL ────
102102
android-dll:

‎Scripts/push_and_run.sh‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ PATH=$PATH:/d/AndroidSDK/platform-tools
33
dotnet build --no-restore
44
adb push StArray.ModManager/bin/Debug/net10.0/StArray.ModManager.dll $TARGET_PATH/StArray.ModManager.dll
55
adb push StArray.ModManager/bin/Debug/net10.0/StArray.ModManager.pdb $TARGET_PATH/StArray.ModManager.pdb
6+
7+
adb push LevelDebugger/bin/Debug/net10.0/LevelDebugger.dll $TARGET_PATH/../mods/LevelDebugger/LevelDebugger.dll
8+
69
adb push StArray.ModManager.Android/bin/Debug/net10.0/StArray.ModManager.Android.dll $TARGET_PATH/StArray.ModManager.Android.dll
7-
adb push StArray.ModManager.Android/bin/Debug/net10.0/StArray.ModManager.Android.pdb $TARGET_PATH/StArray.ModManager.Android.pdb
10+
#adb push StArray.ModManager.Android/bin/Debug/net10.0/StArray.ModManager.Android.pdb $TARGET_PATH/StArray.ModManager.Android.pdb
811
adb shell am force-stop starray.adofai.v3
912
adb shell am start starray.adofai.v3/starray.adofai.v3.MainActivity
1013
adb logcat -c

‎StArray.ModManager.Android/UI/ImGuiEGLRender.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private void InitImGui(IntPtr display, IntPtr surface)
159159
((IImGuiRenderer)this).InitImGui();
160160
ImGuiInputHandler.IsInitialized = true;
161161
// 输入 Hook 委托给静态处理器
162-
ImGuiInputHandler.InstallHooks();
162+
ImGuiInputHandler.InstallInputHooks();
163163
var io = ImGui.GetIO();
164164
io.ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard;
165165
io.FontGlobalScale = 3.0f;

‎StArray.ModManager.Android/UI/ImGuiInputHandler.cs‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,26 @@
22
using System.Runtime.InteropServices;
33
using ImGuiNET;
44
using StArray.ModManager.Android.Native;
5+
using StArray.ModManager.Hooks;
56
using StArray.ModManager.Manager;
67

78
namespace StArray.ModManager.Android.UI;
89

910
/// <summary>ImGui input handler / 输入处理器 — touch/key hooks + IME control</summary>
10-
public static class ImGuiInputHandler
11+
public static partial class ImGuiInputHandler
1112
{
1213
/// <summary>ImGui 上下文就绪后由渲染器设置</summary>
1314
public static bool IsInitialized { get; set; }
14-
15-
private static InitializeMotionEventDelegate s_initializeMotionEvent;
16-
17-
private delegate int InitializeMotionEventDelegate(IntPtr self, IntPtr motionEvent, IntPtr message);
15+
1816

1917
private static bool s_wantTextInputLast;
2018

2119
/// <summary>
2220
/// 安装触摸事件和按键事件 Hook
2321
/// </summary>
24-
public static void InstallHooks()
22+
public static void InstallInputHooks()
2523
{
26-
// —— 触摸事件 Hook ——
27-
string consumerSymbol = "_ZN7android13InputConsumer21initializeMotionEventEPNS_11MotionEventEPKNS_12InputMessageE";
28-
IntPtr consumerAddr = Dobby.SymbolResolver("libinput.so", consumerSymbol);
29-
Dobby.Hook(consumerAddr,
30-
typeof(ImGuiInputHandler).GetMethod(nameof(OnTouchEvent))!.MethodHandle.GetFunctionPointer(),
31-
out var origin);
32-
s_initializeMotionEvent = Marshal.GetDelegateForFunctionPointer<InitializeMotionEventDelegate>(origin);
33-
24+
InstallHooks();
3425
// IME 字符回调:Java nativeSendChar → C → 此回调 → ImGui
3526
NativeFunctions.SetOnAcceptCharCallback(codepoint =>
3627
{
@@ -55,12 +46,20 @@ public static void InstallHooks()
5546
}
5647

5748
/// <summary>触摸事件 Hook 回调</summary>
58-
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
59-
public static int OnTouchEvent(IntPtr self, IntPtr motionEvent, IntPtr message)
49+
[NativeHook("libinput.so","_ZN7android13InputConsumer14consumeSamplesEPNS_26InputEventFactoryInterfaceERNS0_5BatchEmPjPPNS_10InputEventE")]
50+
public unsafe static long OnConsumeSamples(void* thiz,void* factory, IntPtr batch,
51+
ulong count, uint* outSeq, void** outEvent)
52+
{
53+
var result = OnConsumeSamplesOriginal(thiz,factory, batch, count, outSeq, outEvent);
54+
if (IsInitialized && *outEvent != null) ImGuiImplAndroid.HandleInputEvent(new IntPtr(*outEvent));
55+
return result;
56+
}
57+
58+
[NativeHook("libinput.so","_ZN7android13InputConsumer7consumeEPNS_26InputEventFactoryInterfaceEblPjPPNS_10InputEventE")]
59+
public unsafe static long OnConsume(void* thiz, void* factory, bool consumeBatches, ulong frameTime, uint* outSeq, void** outEvent)
6060
{
61-
int result = s_initializeMotionEvent(self, motionEvent, message);
62-
if (IsInitialized)
63-
ImGuiImplAndroid.HandleInputEvent(self);
61+
var result = OnConsumeOriginal(thiz, factory, consumeBatches, frameTime, outSeq, outEvent);
62+
if (IsInitialized && *outEvent != null) ImGuiImplAndroid.HandleInputEvent(new IntPtr(*outEvent));
6463
return result;
6564
}
6665

‎StArray.ModManager.Android/UI/ImGuiVulkanRenderer.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private bool InstallInstance()
221221
Marshal.GetDelegateForFunctionPointer<VkQueuePresentKHRDelegate>(origPresent);
222222

223223
// 输入 Hook
224-
ImGuiInputHandler.InstallHooks();
224+
ImGuiInputHandler.InstallInputHooks();
225225

226226
// 回放缓存的静态事件订阅
227227
if (s_pendingOnRender != null)

0 commit comments

Comments
 (0)