Read devices from the config file.

master
Pacman Ghost 8 years ago
parent b2c000c476
commit 3eff0610c4
  1. 44
      MainApp/AppConfig.cs
  2. 8
      MainApp/DebugConfig.cs
  3. 34
      MainApp/MouseDll.cs
  4. 1
      MainApp/MouseInterception.csproj
  5. 2
      MainApp/Program.cs
  6. 29
      MainApp/Utils.cs
  7. 17
      MouseDll/api.cpp
  8. 28
      MouseDll/api.hpp
  9. 42
      MouseDll/core.cpp
  10. 74
      MouseDll/device.cpp
  11. 49
      MouseDll/device.hpp
  12. 8
      MouseDll/mouse.vcproj

@ -1,6 +1,8 @@
using System.IO ;
using System ;
using System.IO ;
using System.Xml ;
using System.Runtime.InteropServices ;
using System.Collections.Generic ;
namespace MouseInterception
{
@ -10,17 +12,33 @@ namespace MouseInterception
// IMPORTANT! The definitions here must be kept in sync with their C equivalents in api.hpp
[StructLayout( LayoutKind.Sequential , CharSet=CharSet.Unicode , Pack=1 )]
public struct Settings
public struct ApiSettings
{
}
private Settings mSettings ;
public Settings settings { get { return mSettings ; } }
private ApiSettings mSettings ;
public ApiSettings settings { get { return mSettings ; } }
[StructLayout( LayoutKind.Sequential , CharSet=CharSet.Unicode , Pack=1 )]
public struct ApiDevice
{
public int mDeviceId ;
public string mHID ;
public int mDeviceNumber ;
public string mDisplayName ;
public bool mIsEnabled ;
}
private ApiDevice[] mDevices ;
public ApiDevice[] devices { get { return mDevices ; } }
public AppConfig( string fname )
{
// initialize
mSettings = new ApiSettings() ;
mDevices = new ApiDevice[ 0 ] ;
// load the AppConfig
mSettings = new Settings() ;
if ( ! File.Exists( fname ) )
return ;
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings() ;
@ -31,8 +49,22 @@ namespace MouseInterception
using ( XmlReader xmlReader = XmlReader.Create( fname , xmlReaderSettings ) )
xmlDoc.Load( xmlReader ) ;
// parse the values
// parse the app config
XmlNode configXmlNode = xmlDoc.SelectSingleNode( "/config" ) ;
// parse the devices
List<ApiDevice> devices = new List<ApiDevice>() ;
foreach ( XmlNode xn in configXmlNode.SelectNodes("device") )
{
ApiDevice device = new ApiDevice() ;
device.mDeviceId = Int32.Parse( xn.Attributes["id"].Value ) ;
device.mHID = xn.SelectSingleNode("hid").InnerText.Trim() ;
device.mDeviceNumber = Int32.Parse( xn.SelectSingleNode("deviceNumber").InnerText ) ;
device.mDisplayName = Utils.getXmlChildNodeVal( xn , "displayName" ) ;
device.mIsEnabled = Utils.getXmlChildNodeVal( xn , "enabled" , false ) ;
devices.Add( device ) ;
}
mDevices = devices.ToArray() ;
}
}

@ -10,18 +10,18 @@ namespace MouseInterception
// IMPORTANT! The definitions here must be kept in sync with their C equivalents in api.hpp
[StructLayout( LayoutKind.Sequential , CharSet=CharSet.Unicode , Pack=1 )]
public struct Settings
public struct ApiSettings
{
public string mLogFilename ;
}
private Settings mSettings ;
public Settings settings { get { return mSettings ; } }
private ApiSettings mSettings ;
public ApiSettings settings { get { return mSettings ; } }
public DebugConfig( string fname )
{
// load the DebugConfig
mSettings = new Settings() ;
mSettings = new ApiSettings() ;
if ( ! File.Exists( fname ) )
return ;
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings() ;

@ -13,7 +13,10 @@ namespace MouseInterception
[DllImport( @DLL_NAME , CallingConvention=CallingConvention.Cdecl )]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string open_api(
ref AppConfig.Settings pAppSettings , ref DebugConfig.Settings pDebugSettings , int initConsole
ref AppConfig.ApiSettings pAppSettings ,
[MarshalAs(UnmanagedType.LPArray)] AppConfig.ApiDevice[] pDevices , int nDevices ,
ref DebugConfig.ApiSettings pDebugSettings ,
int initConsole
) ;
[DllImport( @DLL_NAME , CallingConvention=CallingConvention.Cdecl )]
@ -22,14 +25,24 @@ namespace MouseInterception
[DllImport( @DLL_NAME , CallingConvention=CallingConvention.Cdecl )]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string reload_config( ref AppConfig.Settings pAppSettings , ref DebugConfig.Settings pDebugSettings ) ;
private static extern string reload_config(
ref AppConfig.ApiSettings pAppSettings ,
[MarshalAs(UnmanagedType.LPArray)] AppConfig.ApiDevice[] pDevices , int nDevices ,
ref DebugConfig.ApiSettings pDebugSettings
) ;
public MouseDll( bool initConsole )
{
// open the mouse API
AppConfig.Settings appSettings = Program.appConfig.settings ;
DebugConfig.Settings debugSettings = Program.debugConfig.settings ;
string errorMsg = open_api( ref appSettings , ref debugSettings , initConsole?1:0 ) ;
AppConfig.ApiSettings appSettings = Program.appConfig.settings ;
AppConfig.ApiDevice[] devices = Program.appConfig.devices ;
DebugConfig.ApiSettings debugSettings = Program.debugConfig.settings ;
string errorMsg = open_api(
ref appSettings ,
devices , devices.Length ,
ref debugSettings ,
initConsole ? 1 : 0
) ;
if ( errorMsg != null )
throw new Exception( errorMsg ) ;
}
@ -45,9 +58,14 @@ namespace MouseInterception
public void reloadConfig()
{
// reload the config
AppConfig.Settings appSettings = Program.appConfig.settings ;
DebugConfig.Settings debugSettings = Program.debugConfig.settings ;
string errorMsg = reload_config( ref appSettings , ref debugSettings ) ;
AppConfig.ApiSettings appSettings = Program.appConfig.settings ;
AppConfig.ApiDevice[] devices = Program.appConfig.devices ;
DebugConfig.ApiSettings debugSettings = Program.debugConfig.settings ;
string errorMsg = reload_config(
ref appSettings ,
devices , devices.Length ,
ref debugSettings
) ;
if ( errorMsg != null )
throw new Exception( errorMsg ) ;
}

@ -87,6 +87,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>strings.resx</DependentUpon>
</Compile>
<Compile Include="Utils.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

@ -36,7 +36,7 @@ namespace MouseInterception
{
// run in console mode
AttachConsole( ATTACH_PARENT_PROCESS ) ;
System.Console.WriteLine( "\nInitialized the C# console." ) ; // FIXME!
System.Console.WriteLine( "\n>>> Initialized the C# console." ) ; // FIXME!
MouseDll mouseDll = new MouseDll( true ) ;
System.Windows.Forms.SendKeys.SendWait( "{ENTER}" ) ;
Application.Exit() ;

@ -0,0 +1,29 @@
using System ;
using System.Xml ;
namespace MouseInterception
{
static class Utils
{
public static string getXmlChildNodeVal( XmlNode xmlNode , string childNodeName , string defaultVal )
{
// get the child node's value
XmlNode childNode = xmlNode.SelectSingleNode( childNodeName ) ;
if ( childNode == null )
return defaultVal ;
return childNode.InnerText.Trim() ;
}
public static string getXmlChildNodeVal( XmlNode xn , string s ) { return getXmlChildNodeVal(xn,s,"") ; }
public static bool getXmlChildNodeVal( XmlNode xmlNode , string childNodeName , bool defaultVal )
{
// get the child node's value
string val = getXmlChildNodeVal( xmlNode , childNodeName , null ) ;
if ( val == null )
return defaultVal ;
return Boolean.Parse( val ) ;
}
}
}

@ -8,12 +8,17 @@ using namespace std ;
// ---------------------------------------------------------------------
extern "C" __declspec(dllexport) BSTR
open_api( const AppConfig* pAppConfig , const DebugConfig* pDebugConfig , int initConsole )
open_api(
const ApiAppConfig* pAppConfig ,
const ApiDevice* pDevices , int nDevices ,
const ApiDebugConfig* pDebugConfig ,
int initConsole
)
{
// open the API
try
{
openApi( pAppConfig , pDebugConfig , initConsole != 0 ) ;
openApi( pAppConfig , pDevices , nDevices , pDebugConfig , initConsole != 0 ) ;
return NULL ;
}
catch ( exception& xcptn )
@ -42,12 +47,16 @@ close_api()
// ---------------------------------------------------------------------
extern "C" __declspec(dllexport) BSTR
reload_config( const AppConfig* pAppConfig , const DebugConfig* pDebugConfig )
reload_config(
const ApiAppConfig* pAppConfig ,
const ApiDevice* pDevices , int nDevices ,
const ApiDebugConfig* pDebugConfig
)
{
// reload the config
try
{
reloadConfig( pAppConfig , pDebugConfig ) ;
reloadConfig( pAppConfig , pDevices , nDevices , pDebugConfig ) ;
return NULL ;
}
catch ( exception& xcptn )

@ -3,15 +3,24 @@
// ---------------------------------------------------------------------
// IMPORTANT! The definitions here must be kept in sync with their C# equivalents in App/DebugConfig.cs.
// IMPORTANT! The definitions here must be kept in sync with their C# equivalents in App/ApiDebugConfig.cs.
struct AppConfig
struct ApiAppConfig
{
} ;
struct ApiDevice
{
int mDeviceId ;
const wchar_t* mpHID ;
int mDeviceNumber ;
const wchar_t* mpDisplayName ;
bool mIsEnabled ;
} ;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct DebugConfig
struct ApiDebugConfig
{
const wchar_t* mpLogFilename ;
} ;
@ -20,10 +29,19 @@ struct DebugConfig
// IMPORTANT! The definitions here must be kept in sync with their C# equivalents in MouseDll.cs.
extern void openApi( const AppConfig* pAppConfig , const DebugConfig* pDebugConfig , bool initConsole ) ;
extern void openApi(
const ApiAppConfig* pAppConfig ,
const ApiDevice* pDevices , int nDevices ,
const ApiDebugConfig* pDebugConfig ,
bool initConsole
) ;
extern void closeApi() ;
extern void reloadConfig( const AppConfig* pAppConfig , const DebugConfig* pDebugConfig ) ;
extern void reloadConfig(
const ApiAppConfig* pAppConfig ,
const ApiDevice* pDevices , int nDevices ,
const ApiDebugConfig* pDebugConfig
) ;
// ---------------------------------------------------------------------

@ -3,6 +3,9 @@
#include <fstream>
#include <stdexcept>
#include <cassert>
#include <map>
#include "device.hpp"
#include "api.hpp"
#include "utils.hpp"
@ -12,6 +15,13 @@ using namespace std ;
static HMODULE ghInterceptionDll = NULL ;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
typedef map< int , Device > DeviceTable ;
static DeviceTable gDeviceTable ;
// --- LOCAL DATA ------------------------------------------------------
static bool gEnableConsole = false ;
static wstring gLogFilename ;
static ofstream gLogFile ;
@ -33,7 +43,12 @@ static ofstream gLogFile ;
// ---------------------------------------------------------------------
void
openApi( const AppConfig* pAppConfig , const DebugConfig* pDebugConfig , bool initConsole )
openApi(
const ApiAppConfig* pAppConfig ,
const ApiDevice* pDevices , int nDevices ,
const ApiDebugConfig* pDebugConfig ,
bool initConsole
)
{
// check if we are open
if ( ghInterceptionDll != NULL )
@ -41,7 +56,7 @@ openApi( const AppConfig* pAppConfig , const DebugConfig* pDebugConfig , bool in
// initialize
gEnableConsole = initConsole ;
reloadConfig( pAppConfig , pDebugConfig ) ;
reloadConfig( pAppConfig , pDevices , nDevices , pDebugConfig ) ;
// load Interception
wchar_t buf[ _MAX_PATH+1 ] ;
@ -78,11 +93,19 @@ closeApi()
// ---------------------------------------------------------------------
void
reloadConfig( const AppConfig* pAppConfig , const DebugConfig* pDebugConfig )
reloadConfig(
const ApiAppConfig* pAppConfig ,
const ApiDevice* pDevices , int nDevices ,
const ApiDebugConfig* pDebugConfig
)
{
// validate the parameters
if ( pAppConfig == NULL )
throw runtime_error( "Missing AppConfig." ) ;
if ( pDevices == NULL )
throw runtime_error( "Missing Device's." ) ;
if ( nDevices < 0 )
throw runtime_error( "Invalid device count." ) ;
if ( pDebugConfig == NULL )
throw runtime_error( "Missing DebugConfig." ) ;
const wchar_t* pLogFilename = pDebugConfig->mpLogFilename ;
@ -100,4 +123,17 @@ reloadConfig( const AppConfig* pAppConfig , const DebugConfig* pDebugConfig )
gLogFilename = pLogFilename ;
}
}
// load the Device's
gDeviceTable.clear() ;
for ( int i=0 ; i < nDevices ; ++i )
{
const ApiDevice* pDevice = pDevices+i ;
gDeviceTable[ pDevice->mDeviceId ] = Device( pDevice ) ;
}
#if 1 // FIXME!
LOG_MSG( "Loaded devices:" ) ;
for ( DeviceTable::const_iterator it=gDeviceTable.begin() ; it != gDeviceTable.end() ; ++it )
(*it).second.dumpDevice( cout , " " ) ;
#endif
}

@ -0,0 +1,74 @@
#include "device.hpp"
#include "api.hpp"
#include "utils.hpp"
using namespace std ;
// --- CONSTRUCTORS ----------------------------------------------------
Device::Device( const ApiDevice* pDevice )
{
// initialize the Device
mDeviceId = pDevice->mDeviceId ;
mHID = toUtf8( pDevice->mpHID ) ;
mDeviceNumber = pDevice->mDeviceNumber ;
mDisplayName = toUtf8( pDevice->mpDisplayName ) ;
mIsEnabled = pDevice->mIsEnabled ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Device::Device()
: mDeviceId(-1)
, mHID() , mDeviceNumber(-1)
, mDisplayName() , mIsEnabled(false)
{
}
// --- COPY/ASSIGNMENT -------------------------------------------------
Device& Device::operator=( const Device& rhs )
{
// copy the Device
mDeviceId = rhs.deviceId() ;
mHID = rhs.hid() ;
mDeviceNumber = rhs.deviceNumber() ;
mDisplayName = rhs.displayName() ;
mIsEnabled = rhs.isEnabled() ;
return *this ;
}
Device::Device( const Device& rhs ) { operator=(rhs) ; }
// ---------------------------------------------------------------------
void
Device::dumpDevice( ostream& os , const char* pPrefix ) const
{
// dump the device
if ( pPrefix == NULL )
pPrefix = "" ;
os << pPrefix << "Device (id=" << deviceId() << "):" << endl ;
os << pPrefix << "- hid = \"" << hid() << "\"" << endl ;
os << pPrefix << "- deviceNumber = " << deviceNumber() << endl ;
os << pPrefix << "- displayName = \"" << displayName() << "\"" << endl ;
os << pPrefix << "- isEnabled = " << isEnabled() << endl ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ostream&
operator<<( ostream& os , const Device& device )
{
// insert the Device
os << "[" << device.deviceId() << ":" << device.hid() << "/" << device.deviceNumber() << "]" ;
return os ;
}
// ---------------------------------------------------------------------
int Device::deviceId() const { return mDeviceId ; }
const string& Device::hid() const { return mHID ; }
int Device::deviceNumber() const { return mDeviceNumber ; }
const string& Device::displayName() const { return mDisplayName ; }
bool Device::isEnabled() const { return mIsEnabled ; }

@ -0,0 +1,49 @@
#ifndef DEVICE_HPP
#define DEVICE_HPP
#include <string>
#include <ostream>
struct ApiDevice ;
// ---------------------------------------------------------------------
class Device
{
// constructors/destructor:
public:
Device( const ApiDevice* pDevice ) ;
Device() ;
// copy/assignment:
public:
Device( const Device& rhs ) ;
Device& operator=( const Device& rhs ) ;
// access methods:
public:
int deviceId() const ;
const std::string& hid() const ;
int deviceNumber() const ;
const std::string& displayName() const ;
bool isEnabled() const ;
// miscellaneous methods:
public:
void dumpDevice( std::ostream& os , const char* pPrefix="" ) const ;
// data members:
private:
int mDeviceId ;
std::string mHID ;
int mDeviceNumber ;
std::string mDisplayName ;
bool mIsEnabled ;
} ;
std::ostream& operator<<( std::ostream& , const Device& ) ;
// ---------------------------------------------------------------------
#endif // DEVICE_HPP

@ -184,6 +184,10 @@
RelativePath=".\core.cpp"
>
</File>
<File
RelativePath=".\device.cpp"
>
</File>
<File
RelativePath=".\dllMain.cpp"
>
@ -202,6 +206,10 @@
RelativePath=".\api.hpp"
>
</File>
<File
RelativePath=".\device.hpp"
>
</File>
<File
RelativePath=".\utils.hpp"
>

Loading…
Cancel
Save