Detect which way the device is being moved.

master
Pacman Ghost 7 years ago
parent 3a1b91f74d
commit 30698ac62a
  1. 5
      MouseDll/actions.cpp
  2. 71
      MouseDll/core2.cpp
  3. 52
      MouseDll/event.cpp
  4. 17
      MouseDll/globals.cpp
  5. 5
      MouseDll/globals.hpp

@ -7,6 +7,11 @@ using namespace std ;
Action::Action( const ApiAction* pAction )
{
// NOTE: These relationships are not *necessary*, but let's keep things consistent.
assert( atMouseLeft == dLeft ) ;
assert( atMouseRight == dRight ) ;
assert( atMouseUp == dUp ) ;
assert( atMouseDown == dDown ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

@ -1,11 +1,21 @@
#include <deque>
#include "globals.hpp"
using namespace std ;
typedef deque<InterceptionMouseStroke> InterceptionMouseStrokeDeque ;
typedef map< InterceptionDevice , InterceptionMouseStrokeDeque > InterceptionMouseStrokeHistory ;
// --- LOCAL DATA ------------------------------------------------------
#define MAX_STROKE_HISTORY 20
#define DIRN_DETECT_WINDOW_SIZE 10
#define DIRN_DETECT_HORZ_BIAS 1.2 // FIXME! s.b. configurable
// local functions:
static void doRunMainLoop( int* pExitFlag ) ;
static bool detectDirn( const InterceptionMouseStrokeDeque* pStrokeHistory , eDirn* pDirn , int* pMagnitude ) ;
static bool findDevice( InterceptionDevice hDevice , Device** ppDevice ) ;
// ---------------------------------------------------------------------
@ -62,8 +72,10 @@ doRunMainLoop( int* pExitFlag )
INTERCEPTION_FILTER_MOUSE_MOVE | INTERCEPTION_FILTER_MOUSE_WHEEL | INTERCEPTION_FILTER_MOUSE_HWHEEL
) ;
// initialize
InterceptionMouseStrokeHistory strokeHistory ;
// run the main loop
InterceptionStroke stroke ;
for ( ; ; )
{
// wait for the next event
@ -75,6 +87,7 @@ doRunMainLoop( int* pExitFlag )
break ;
continue ;
}
InterceptionStroke stroke ;
int nStrokes = interception_receive( hContext , hDevice , &stroke , 1 ) ;
if ( nStrokes <= 0 )
break ;
@ -143,6 +156,17 @@ doRunMainLoop( int* pExitFlag )
continue ;
}
// record the stroke
InterceptionMouseStrokeDeque* pStrokeHistory = & strokeHistory[hDevice] ;
pStrokeHistory->push_back( *pStroke ) ;
while ( pStrokeHistory->size() > MAX_STROKE_HISTORY )
pStrokeHistory->pop_front() ;
// figure out which way the mouse is moving
eDirn dirn ;
int dirnMagnitude ;
(void) detectDirn( pStrokeHistory , &dirn , &dirnMagnitude ) ;
// FIXME! handle the event
interception_send( hContext , hDevice ,&stroke , 1 ) ;
}
@ -156,6 +180,51 @@ doRunMainLoop( int* pExitFlag )
// ---------------------------------------------------------------------
static bool
detectDirn( const InterceptionMouseStrokeDeque* pStrokeHistory , eDirn* pDirn , int* pDirnMagnitude )
{
// check if we have enough stroke history
if ( pStrokeHistory->size() < DIRN_DETECT_WINDOW_SIZE )
{
LOG_CMSG( "dirnDetect" , "DIRN DETECT: #=" << pStrokeHistory->size() << " ; dirn=" << toString(dUnknown) ) ;
return false ;
}
// figure out which direction the mouse is moving in
// FIXME! limit to left/right for h-wheel
LOG_CMSG( "dirnDetect2" , "DIRN DETECT: #=" << DIRN_DETECT_WINDOW_SIZE << "/" << pStrokeHistory->size() ) ;
int cumX=0 , cumY=0 , nStrokes=0 ;
for ( InterceptionMouseStrokeDeque::const_reverse_iterator it=pStrokeHistory->rbegin() ; it != pStrokeHistory->rend() ; ++it )
{
const InterceptionMouseStroke& stroke = *it ;
cumX += (*it).x ;
cumY += (*it).y ;
LOG_CMSG( "dirnDetect2" , " x=" << (*it).x << " ; y=" << (*it).y << " ; cum=" << cumX << "/" << cumY ) ;
if ( ++nStrokes >= DIRN_DETECT_WINDOW_SIZE )
break ;
}
// NOTE: It's easier to move a device up/down without drifting left/right, so we bias left/right detection.
int biasedCumX = (int)(1000*cumX * DIRN_DETECT_HORZ_BIAS) / 1000 ;
if ( abs(biasedCumX) >= abs(cumY) )
{
*pDirn = biasedCumX < 0 ? dLeft : dRight ;
*pDirnMagnitude = biasedCumX / nStrokes ;
}
else
{
*pDirn = cumY < 0 ? dUp : dDown ;
*pDirnMagnitude = cumY / nStrokes ;
}
LOG_CMSG( "dirnDetect" ,
"DIRN DETECT: #=" << DIRN_DETECT_WINDOW_SIZE << "/" << pStrokeHistory->size()
<< " ; cumX=" << biasedCumX << "(" << cumX << ") ; cumY=" << cumY
<< " ; dirn=" << toString(*pDirn) << "/" << *pDirnMagnitude
) ;
return true ;
}
// ---------------------------------------------------------------------
static bool
findDevice( InterceptionDevice hDevice , Device** ppDevice )
{

@ -3,29 +3,16 @@
using namespace std ;
// --- LOCAL DATA ------------------------------------------------------
static EnumStringInfo gEventTypeStringTable[] =
{
{ Event::etMouseLeft , "mouseLeft" } ,
{ Event::etMouseRight , "mouseRight" } ,
{ Event::etMouseUp , "mouseUp" } ,
{ Event::etMouseDown , "mouseDown" } ,
{ -1 , NULL }
} ;
static BitFlagsInfo gKeyModifiersStringTable[] =
{
{ Event::kmCtrl , "Ctrl" } ,
{ Event::kmAlt , "Alt" } ,
{ Event::kmShift , "Shift" } ,
{ -1 , NULL } ,
} ;
// --- CONSTRUCTORS ----------------------------------------------------
Event::Event( const ApiEvent* pEvent , const ApiAction* pActions , int nActions )
{
// NOTE: These relationships are not *necessary*, but let's keep things consistent.
assert( etMouseLeft == dLeft ) ;
assert( etMouseRight == dRight ) ;
assert( etMouseUp == dUp ) ;
assert( etMouseDown == dDown ) ;
// initialize the Event
mEventType = pEvent->mEventType ;
mKeyModifiers = pEvent->mKeyModifiers ;
@ -51,8 +38,17 @@ Event::dumpEvent( ostream& os , const char* pPrefix ) const
ostream&
operator<<( ostream& os , const Event& evt )
{
static EnumStringInfo stringTable[] =
{
{ Event::etMouseLeft , "mouseLeft" } ,
{ Event::etMouseRight , "mouseRight" } ,
{ Event::etMouseUp , "mouseUp" } ,
{ Event::etMouseDown , "mouseDown" } ,
{ -1 , NULL }
} ;
// insert the Event
os << "{Event:" << enumString(gEventTypeStringTable,evt.eventType()) ;
os << "{Event:" << enumString(stringTable,evt.eventType()) ;
if ( evt.keyModifiers() != 0 )
os << ":" << Event::keyModifiersString(evt.keyModifiers()) ;
os << "}" ;
@ -61,7 +57,21 @@ operator<<( ostream& os , const Event& evt )
// ---------------------------------------------------------------------
string Event::keyModifiersString( int km ) { return bitFlagsString(gKeyModifiersStringTable,km,'+') ; }
string
Event::keyModifiersString( int keyModifiers )
{
// return the KeyModifiers as a string
static BitFlagsInfo stringTable[] =
{
{ Event::kmCtrl , "Ctrl" } ,
{ Event::kmAlt , "Alt" } ,
{ Event::kmShift , "Shift" } ,
{ -1 , NULL } ,
} ;
return bitFlagsString( stringTable , keyModifiers , '+' ) ;
}
// ---------------------------------------------------------------------
int Event::eventType() const { return mEventType ; }
int Event::keyModifiers() const { return mKeyModifiers ; }

@ -91,6 +91,23 @@ makeLogMsg( const string& msg )
// ---------------------------------------------------------------------
string toString( eDirn dirn )
{
// return the eDirn as a string
static EnumStringInfo stringTable[] =
{
{ dUnknown , "UNKNOWN" } ,
{ dLeft , "LEFT" } ,
{ dRight , "RIGHT" } ,
{ dUp , "UP" } ,
{ dDown , "DOWN" } ,
{ -1 , NULL }
} ;
return enumString( stringTable , dirn ) ;
}
// ---------------------------------------------------------------------
ostream&
operator<<( ostream& os , const exception& xcptn )
{

@ -20,6 +20,11 @@ typedef std::set<std::string> StringSet ;
// ---------------------------------------------------------------------
enum eDirn { dUnknown=-1 , dLeft=1 , dRight=2 , dUp=3 , dDown=4 } ;
extern std::string toString( eDirn ) ;
// ---------------------------------------------------------------------
extern HMODULE ghInterceptionDll ;
typedef IntPtrMap<Device> DeviceTable ;

Loading…
Cancel
Save