@ -20,33 +20,6 @@ namespace AutoHotInterception
private static readonly ConcurrentDictionary < int , IDeviceHandler > DeviceHandlers = new ConcurrentDictionary < int , IDeviceHandler > ( ) ;
private static readonly ConcurrentDictionary < int , ConcurrentDictionary < ushort , MappingOptions > > KeyboardKeyMappings =
new ConcurrentDictionary < int , ConcurrentDictionary < ushort , MappingOptions > > ( ) ;
private static readonly ConcurrentDictionary < int , MappingOptions > KeyboardMappings =
new ConcurrentDictionary < int , MappingOptions > ( ) ;
private static readonly ConcurrentDictionary < int , ConcurrentDictionary < ushort , MappingOptions > > MouseButtonMappings =
new ConcurrentDictionary < int , ConcurrentDictionary < ushort , MappingOptions > > ( ) ;
private static readonly ConcurrentDictionary < int , MappingOptions > MouseButtonsMappings =
new ConcurrentDictionary < int , MappingOptions > ( ) ;
private static readonly ConcurrentDictionary < int , MappingOptions > MouseMoveAbsoluteMappings =
new ConcurrentDictionary < int , MappingOptions > ( ) ;
private static readonly ConcurrentDictionary < int , MappingOptions > MouseMoveRelativeMappings =
new ConcurrentDictionary < int , MappingOptions > ( ) ;
// If an event is subscribed to with concurrent set to false then use a single worker thread to process each event.
// Makes sure the events are handled synchronously and with a FIFO order.
private static readonly ConcurrentDictionary < int , ConcurrentDictionary < ushort , WorkerThread > > WorkerThreads =
new ConcurrentDictionary < int , ConcurrentDictionary < ushort , WorkerThread > > ( ) ;
private static readonly ConcurrentDictionary < int , WorkerThread > DeviceWorkerThreads =
new ConcurrentDictionary < int , WorkerThread > ( ) ;
private static bool _absoluteMode00Reported ;
private static bool _pollThreadRunning ;
private CancellationTokenSource _cancellationToken ;
@ -550,7 +523,6 @@ namespace AutoHotInterception
/// <returns></returns>
private static int IsMonitoredDevice ( int device )
{
//return Convert.ToInt32(FilteredDevices.ContainsKey(device));
return DeviceHandlers [ device ] . IsFiltered ( ) ;
}
@ -560,27 +532,6 @@ namespace AutoHotInterception
state ? ManagedWrapper . Filter . All : ManagedWrapper . Filter . None ) ;
}
private void SetDeviceFilterState ( int device , bool state )
{
//if (state && !FilteredDevices.ContainsKey(device))
// FilteredDevices[device] = true;
//else if (!state && FilteredDevices.ContainsKey(device))
// FilteredDevices.TryRemove(device, out _);
DeviceHandlers [ device ] . SetFilterState ( state ) ;
}
private bool DeviceHasBindings ( int id )
{
if ( id < 11 )
return KeyboardKeyMappings . ContainsKey ( id ) ;
return MouseButtonMappings . ContainsKey ( id )
| | MouseMoveRelativeMappings . ContainsKey ( id )
| | MouseMoveAbsoluteMappings . ContainsKey ( id ) ;
}
// ScanCode notes: https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html
private static void PollThread ( object obj )
{
var token = ( CancellationToken ) obj ;
@ -596,231 +547,6 @@ namespace AutoHotInterception
while ( ManagedWrapper . Receive ( DeviceContext , i = ManagedWrapper . WaitWithTimeout ( DeviceContext , 10 ) , ref stroke , 1 ) > 0 )
{
DeviceHandlers [ i ] . ProcessStroke ( stroke ) ;
if ( false )
{
if ( i < 11 )
{
}
else
{
// Mice
var hasSubscription = false ;
var hasContext = ContextCallbacks . ContainsKey ( i ) ;
var moveRemoved = false ;
var hasMove = false ;
var x = stroke . mouse . x ;
var y = stroke . mouse . y ;
//Debug.WriteLine($"AHK| Stroke Seen. State = {stroke.mouse.state}, Flags = {stroke.mouse.flags}, x={x}, y={y}");
// Process mouse movement
var isAbsolute = ( stroke . mouse . flags & ( ushort ) ManagedWrapper . MouseFlag . MouseMoveAbsolute ) = =
( ushort ) ManagedWrapper . MouseFlag . MouseMoveAbsolute ;
//Determine whether or not to report mouse movement.
// For Relative mode, this is fairly simple - if x and y are both 0, no movement was reported (Since a real mouse never reports x=0/y=0)
// For Absolute mode, x=0/y=0 is reported, but we should limit this to only reporting once...
// ... so when x=0/y=0 is seen in absolute mode, set the flag _absoluteMode00Reported to true and allow it to be reported...
// then on subsequent reports of x=0/y=0 for absolute mode, if _absoluteMode00Reported is already true, then do not report movement...
// ... In absolute mode, when x!=0/y!=0 is received, clear the _absoluteMode00Reported flag
if ( isAbsolute )
{
if ( x = = 0 & & y = = 0 )
{
if ( ! _absoluteMode00Reported )
{
hasMove = true ;
_absoluteMode00Reported = true ;
}
else
{
hasMove = false ;
}
}
else
{
hasMove = true ;
_absoluteMode00Reported = false ;
}
}
else
{
hasMove = ( x ! = 0 | | y ! = 0 ) ;
}
if ( hasMove )
{
// Process Absolute Mouse Move
if ( isAbsolute )
{
if ( MouseMoveAbsoluteMappings . ContainsKey ( i ) )
{
var mapping = MouseMoveAbsoluteMappings [ i ] ;
hasSubscription = true ;
//var debugStr = $"AHK| Mouse stroke has absolute move of {x}, {y}...";
if ( mapping . Concurrent )
ThreadPool . QueueUserWorkItem ( threadProc = > mapping . Callback ( x , y ) ) ;
else if ( WorkerThreads . ContainsKey ( i ) & & WorkerThreads [ i ] . ContainsKey ( 7 ) )
WorkerThreads [ i ] [ 7 ] ? . Actions . Add ( ( ) = > mapping . Callback ( x , y ) ) ;
if ( mapping . Block )
{
moveRemoved = true ;
stroke . mouse . x = 0 ;
stroke . mouse . y = 0 ;
//debugStr += "Blocking";
}
else
{
//debugStr += "Not Blocking";
}
//Debug.WriteLine(debugStr);
}
}
// Process Relative Mouse Move
//else if ((stroke.mouse.flags & (ushort) ManagedWrapper.MouseFlag.MouseMoveRelative) == (ushort) ManagedWrapper.MouseFlag.MouseMoveRelative) / flag is 0, so always true!
else
{
if ( MouseMoveRelativeMappings . ContainsKey ( i ) )
{
var mapping = MouseMoveRelativeMappings [ i ] ;
hasSubscription = true ;
//var debugStr = $"AHK| Mouse stroke has relative move of {x}, {y}...";
if ( mapping . Concurrent )
ThreadPool . QueueUserWorkItem ( threadProc = > mapping . Callback ( x , y ) ) ;
else if ( WorkerThreads . ContainsKey ( i ) & & WorkerThreads [ i ] . ContainsKey ( 8 ) )
WorkerThreads [ i ] [ 8 ] ? . Actions . Add ( ( ) = > mapping . Callback ( x , y ) ) ;
if ( mapping . Block )
{
moveRemoved = true ;
stroke . mouse . x = 0 ;
stroke . mouse . y = 0 ;
//debugStr += "Blocking";
}
else
{
//debugStr += "Not Blocking";
}
//Debug.WriteLine(debugStr);
}
}
}
var isMouseButtonsMapping = MouseButtonsMappings . ContainsKey ( i ) ;
// Process Mouse Buttons - do this AFTER mouse movement, so that absolute mode has coordinates available at the point that the button callback is fired
if ( stroke . mouse . state ! = 0 & & MouseButtonMappings . ContainsKey ( i ) | | isMouseButtonsMapping )
{
var btnStates = HelperFunctions . MouseStrokeToButtonStates ( stroke ) ;
foreach ( var btnState in btnStates )
{
if ( ! isMouseButtonsMapping & & ! MouseButtonMappings [ i ] . ContainsKey ( btnState . Button ) )
continue ;
hasSubscription = true ;
MappingOptions mapping = null ;
if ( isMouseButtonsMapping )
{
mapping = MouseButtonsMappings [ i ] ;
}
else
{
mapping = MouseButtonMappings [ i ] [ btnState . Button ] ;
}
var state = btnState ;
if ( mapping . Concurrent )
{
if ( isMouseButtonsMapping )
{
ThreadPool . QueueUserWorkItem ( threadProc = >
mapping . Callback ( btnState . Button , state . State ) ) ;
}
else
{
ThreadPool . QueueUserWorkItem ( threadProc = > mapping . Callback ( state . State ) ) ;
}
}
else
{
if ( isMouseButtonsMapping )
{
DeviceWorkerThreads [ i ] ? . Actions
. Add ( ( ) = > mapping . Callback ( btnState . Button , state . State ) ) ;
}
else
{
WorkerThreads [ i ] [ btnState . Button ] ? . Actions
. Add ( ( ) = > mapping . Callback ( state . State ) ) ;
}
}
if ( mapping . Block )
{
// Remove the event for this button from the stroke, leaving other button events intact
stroke . mouse . state - = btnState . Flag ;
// If we are removing a mouse wheel event, then set rolling to 0 if no mouse wheel event left
if ( btnState . Flag = = 0x400 | | btnState . Flag = = 0x800 )
{
if ( ( stroke . mouse . state & 0x400 ) ! = 0x400 & &
( stroke . mouse . state & 0x800 ) ! = 0x800 )
{
//Debug.WriteLine("AHK| Removing rolling flag from stroke");
stroke . mouse . rolling = 0 ;
}
}
//Debug.WriteLine($"AHK| Removing flag {btnState.Flag} from stoke, leaving state {stroke.mouse.state}");
}
else
{
//Debug.WriteLine($"AHK| Leaving flag {btnState.Flag} in stroke");
}
}
}
// Forward on the stroke if required
if ( hasSubscription )
{
// Subscription mode
// If the stroke has a move that was not removed, OR it has remaining button events, then forward on the stroke
if ( ( hasMove & & ! moveRemoved ) | | stroke . mouse . state ! = 0 )
{
//Debug.WriteLine($"AHK| Sending stroke. State = {stroke.mouse.state}. hasMove={hasMove}, moveRemoved={moveRemoved}");
ManagedWrapper . Send ( DeviceContext , i , ref stroke , 1 ) ;
}
else
{
// Everything removed from stroke, do not forward
//Debug.WriteLine("AHK| Mouse stroke now empty, not forwarding");
}
}
else if ( hasContext )
{
// Context Mode - forward stroke with context wrapping
ContextCallbacks [ i ] ( 1 ) ;
ManagedWrapper . Send ( DeviceContext , i , ref stroke , 1 ) ;
ContextCallbacks [ i ] ( 0 ) ;
}
else
{
// No subscription or context mode - forward on
//Debug.WriteLine($"AHK| Sending stroke. State = {stroke.mouse.state}. hasMove={hasMove}, moveRemoved={moveRemoved}");
ManagedWrapper . Send ( DeviceContext , i , ref stroke , 1 ) ;
}
//Debug.WriteLine($"AHK| ");
}
}
}
}
_pollThreadRunning = false ;