22using System . Runtime . InteropServices ;
33using ImGuiNET ;
44using StArray . ModManager . Android . Native ;
5+ using StArray . ModManager . Hooks ;
56using StArray . ModManager . Manager ;
67
78namespace 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
0 commit comments