77using BizHawk . Common ;
88using BizHawk . Common . CollectionExtensions ;
99
10+ using Windows . Win32 ;
11+ using Windows . Win32 . Foundation ;
12+ using Windows . Win32 . UI . WindowsAndMessaging ;
13+
1014using static BizHawk . Common . RawInputImports ;
11- using static BizHawk . Common . WmImports ;
15+ using static BizHawk . Common . WmImports1 ;
16+ using static Windows . Win32 . Win32Imports ;
1217
1318namespace BizHawk . Bizware . Input
1419{
1520 /// <summary>
1621 /// Note: Only 1 window per device class (e.g. keyboards) is actually allowed to use RAWINPUT (last one to call RegisterRawInputDevices)
1722 /// So only one instance can actually be used at the same time
1823 /// </summary>
19- internal sealed class RawKeyMouseInput : IKeyMouseInput
24+ internal sealed unsafe class RawKeyMouseInput : IKeyMouseInput
2025 {
2126 private const int WM_CLOSE = 0x0010 ;
2227 private const int WM_INPUT = 0x00FF ;
2328
24- private IntPtr RawInputWindow ;
29+ private HWND RawInputWindow ;
2530 private bool _handleAltKbLayouts ;
2631 private List < KeyEvent > _keyEvents = [ ] ;
2732 private ( int X , int Y ) _mouseDelta ;
@@ -33,27 +38,29 @@ internal sealed class RawKeyMouseInput : IKeyMouseInput
3338 private int RawInputBufferSize ;
3439 private readonly int RawInputBufferDataOffset ;
3540
36- private static readonly WNDPROC _wndProc = WndProc ;
37-
38- private static readonly Lazy < IntPtr > _rawInputWindowAtom = new ( ( ) =>
41+ private static unsafe readonly Lazy < PCWSTR > _rawInputWindowAtom = new ( ( ) =>
3942 {
40- var wc = default ( WNDCLASSW ) ;
41- wc . lpfnWndProc = _wndProc ;
42- wc . hInstance = LoaderApiImports . GetModuleHandleW ( null ) ;
43- wc . lpszClassName = "RawKeyMouseInputClass" ;
44-
45- var atom = RegisterClassW ( ref wc ) ;
46- if ( atom == IntPtr . Zero )
43+ WNDCLASSW wc = default ;
44+ wc . lpfnWndProc = WndProc ;
45+ wc . hInstance = GetModuleHandleW ( default ( PCWSTR ) ) ;
46+ var lpszClassNameStr = "RawKeyMouseInputClass" ;
47+ PCWSTR atom ;
48+ fixed ( char * lpszClassName = lpszClassNameStr )
49+ {
50+ wc . lpszClassName = lpszClassName ;
51+ atom = MAKEINTATOM ( RegisterClassW ( in wc ) ) ;
52+ }
53+ if ( atom . Value is null )
4754 {
4855 throw new InvalidOperationException ( "Failed to register RAWINPUT window class" ) ;
4956 }
5057
5158 return atom ;
5259 } ) ;
5360
54- private static unsafe IntPtr WndProc ( IntPtr hWnd , uint uMsg , IntPtr wParam , IntPtr lParam )
61+ private static LRESULT WndProc ( HWND hWnd , uint uMsg , WPARAM wParam , LPARAM lParam )
5562 {
56- var ud = GetWindowLongPtrW ( hWnd , GWLP_USERDATA ) ;
63+ var ud = GetWindowLongPtrW ( hWnd , WINDOW_LONG_PTR_INDEX . GWLP_USERDATA ) ;
5764 if ( ud == IntPtr . Zero )
5865 {
5966 return DefWindowProcW ( hWnd , uMsg , wParam , lParam ) ;
@@ -65,7 +72,7 @@ private static unsafe IntPtr WndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntP
6572 {
6673 if ( uMsg == WM_CLOSE )
6774 {
68- SetWindowLongPtrW ( hWnd , GWLP_USERDATA , IntPtr . Zero ) ;
75+ SetWindowLongPtrW ( hWnd , WINDOW_LONG_PTR_INDEX . GWLP_USERDATA , IntPtr . Zero ) ;
6976 handle = GCHandle . FromIntPtr ( ud ) ;
7077 rawKeyMouseInput = ( RawKeyMouseInput ) handle . Target ;
7178 Marshal . FreeCoTaskMem ( rawKeyMouseInput . RawInputBuffer ) ;
@@ -157,7 +164,7 @@ private static unsafe IntPtr WndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntP
157164 }
158165 }
159166
160- return IntPtr . Zero ;
167+ return default ;
161168 }
162169
163170 private unsafe void AddKeyInput ( RAWKEYBOARD * keyboard )
@@ -203,24 +210,27 @@ private unsafe void AddMouseInput(RAWMOUSE* mouse)
203210 }
204211 }
205212
206- private static IntPtr CreateRawInputWindow ( )
213+ private static unsafe HWND CreateRawInputWindow ( )
207214 {
208- const int WS_CHILD = 0x40000000 ;
209- var window = CreateWindowExW (
215+ var lpWindowNameStr = "RawKeyInput" ;
216+ HWND window ;
217+ fixed ( char * lpWindowName = lpWindowNameStr )
218+ {
219+ window = CreateWindowExW (
210220 dwExStyle : 0 ,
211221 lpClassName : _rawInputWindowAtom . Value ,
212- lpWindowName : "RawKeyInput" ,
213- dwStyle : WS_CHILD ,
222+ lpWindowName : lpWindowName ,
223+ dwStyle : WINDOW_STYLE . WS_CHILD ,
214224 X : 0 ,
215225 Y : 0 ,
216226 nWidth : 1 ,
217227 nHeight : 1 ,
218- hWndParent : HWND_MESSAGE ,
219- hMenu : IntPtr . Zero ,
220- hInstance : LoaderApiImports . GetModuleHandleW ( null ) ,
221- lpParam : IntPtr . Zero ) ;
222-
223- if ( window == IntPtr . Zero )
228+ hWndParent : HWND . HWND_MESSAGE ,
229+ hMenu : default ,
230+ hInstance : GetModuleHandleW ( default ( PCWSTR ) ) ,
231+ lpParam : default ) ;
232+ }
233+ if ( window . IsNull )
224234 {
225235 throw new InvalidOperationException ( "Failed to create RAWINPUT window" ) ;
226236 }
@@ -280,11 +290,11 @@ public void Dispose()
280290 {
281291 lock ( _lockObj )
282292 {
283- if ( RawInputWindow != IntPtr . Zero )
293+ if ( ! RawInputWindow . IsNull )
284294 {
285295 // Can't use DestroyWindow, that's only allowed in the thread that created the window!
286- PostMessageW ( RawInputWindow , WM_CLOSE , IntPtr . Zero , IntPtr . Zero ) ;
287- RawInputWindow = IntPtr . Zero ;
296+ PostMessageW ( RawInputWindow , WM_CLOSE , default , default ) ;
297+ RawInputWindow = HWND . Null ;
288298 }
289299 else
290300 {
@@ -310,15 +320,20 @@ public IEnumerable<KeyEvent> UpdateKeyInputs(bool handleAltKbLayouts)
310320 {
311321 RawInputWindow = CreateRawInputWindow ( ) ;
312322 var handle = GCHandle . Alloc ( this , GCHandleType . Normal ) ;
313- SetWindowLongPtrW ( RawInputWindow , GWLP_USERDATA , GCHandle . ToIntPtr ( handle ) ) ;
323+ SetWindowLongPtrW ( RawInputWindow , WINDOW_LONG_PTR_INDEX . GWLP_USERDATA , GCHandle . ToIntPtr ( handle ) ) ;
314324 }
315325
316326 _handleAltKbLayouts = handleAltKbLayouts ;
317327
318- while ( PeekMessageW ( out var msg , RawInputWindow , 0 , 0 , PM_REMOVE ) )
328+ while ( PeekMessageW (
329+ out var msg ,
330+ RawInputWindow ,
331+ wMsgFilterMin : 0 ,
332+ wMsgFilterMax : 0 ,
333+ PEEK_MESSAGE_REMOVE_TYPE . PM_REMOVE ) )
319334 {
320- TranslateMessage ( ref msg ) ;
321- DispatchMessageW ( ref msg ) ;
335+ TranslateMessage ( in msg ) ;
336+ DispatchMessageW ( in msg ) ;
322337 }
323338
324339 var ret = _keyEvents ;
@@ -340,13 +355,13 @@ public IEnumerable<KeyEvent> UpdateKeyInputs(bool handleAltKbLayouts)
340355 {
341356 RawInputWindow = CreateRawInputWindow ( ) ;
342357 var handle = GCHandle . Alloc ( this , GCHandleType . Normal ) ;
343- SetWindowLongPtrW ( RawInputWindow , GWLP_USERDATA , GCHandle . ToIntPtr ( handle ) ) ;
358+ SetWindowLongPtrW ( RawInputWindow , WINDOW_LONG_PTR_INDEX . GWLP_USERDATA , GCHandle . ToIntPtr ( handle ) ) ;
344359 }
345360
346- while ( PeekMessageW ( out var msg , RawInputWindow , 0 , 0 , PM_REMOVE ) )
361+ while ( PeekMessageW ( out var msg , RawInputWindow , 0 , 0 , PEEK_MESSAGE_REMOVE_TYPE . PM_REMOVE ) )
347362 {
348- TranslateMessage ( ref msg ) ;
349- DispatchMessageW ( ref msg ) ;
363+ TranslateMessage ( in msg ) ;
364+ DispatchMessageW ( in msg ) ;
350365 }
351366
352367 var ret = _mouseDelta ;
0 commit comments