-
Notifications
You must be signed in to change notification settings - Fork 3
/
LogitechGkeySDK.cpp
112 lines (86 loc) · 2.65 KB
/
LogitechGkeySDK.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "nil.h"
#include "nilLogitech.h"
#include "nilUtil.h"
# define NIL_LOAD_SDK_FUNC(x) mFunctions.pfn##x##=(fn##x##)GetProcAddress(mModule,#x)
namespace Nil {
namespace Logitech {
const wchar_t* cLogitechGKeyModuleName = L"LogitechGkey.dll";
GKeySDK::Functions::Functions():
pfnLogiGkeyInit( nullptr ),
pfnLogiGkeyGetMouseButtonString( nullptr ),
pfnLogiGkeyGetKeyboardGkeyString( nullptr ),
pfnLogiGkeyShutdown( nullptr )
{
}
GKeySDK::GKeySDK(): ExternalModule()
{
InitializeSRWLock( &mLock );
}
void GKeySDK::addListener( GKeyListener* listener )
{
mListeners.push_back( listener );
}
void GKeySDK::removeListener( GKeyListener* listener )
{
mListeners.remove( listener );
}
GKeySDK::InitReturn GKeySDK::initialize()
{
mModule = LoadLibraryW( cLogitechGKeyModuleName );
if ( !mModule )
return Initialization_ModuleNotFound;
NIL_LOAD_SDK_FUNC( LogiGkeyInit );
NIL_LOAD_SDK_FUNC( LogiGkeyGetMouseButtonString );
NIL_LOAD_SDK_FUNC( LogiGkeyGetKeyboardGkeyString );
NIL_LOAD_SDK_FUNC( LogiGkeyShutdown );
if ( !mFunctions.pfnLogiGkeyInit
|| !mFunctions.pfnLogiGkeyGetMouseButtonString
|| !mFunctions.pfnLogiGkeyGetKeyboardGkeyString
|| !mFunctions.pfnLogiGkeyShutdown )
return Initialization_MissingExports;
mContext.gkeyContext = this;
mContext.gkeyCallBack = keyCallback;
if ( !mFunctions.pfnLogiGkeyInit( &mContext ) )
return Initialization_Unavailable;
mInitialized = true;
return Initialization_OK;
}
void GKeySDK::keyCallback( GkeyCode key, const wchar_t* name, void* context ) //-V813
{
auto sdk = reinterpret_cast<GKeySDK*>( context );
ScopedSRWLock lock( &sdk->mLock );
sdk->mQueue.push( key );
}
void GKeySDK::update()
{
ScopedSRWLock lock( &mLock );
while ( !mQueue.empty() )
{
for ( auto listener : mListeners )
{
if ( mQueue.front().keyDown )
listener->onGKeyPressed( mQueue.front().keyIdx );
else
listener->onGKeyReleased( mQueue.front().keyIdx );
}
mQueue.pop();
}
}
void GKeySDK::shutdown()
{
ScopedSRWLock lock( &mLock );
if ( mModule )
{
if ( mInitialized )
mFunctions.pfnLogiGkeyShutdown();
FreeLibrary( mModule );
mModule = NULL;
}
mInitialized = false;
}
GKeySDK::~GKeySDK()
{
shutdown();
}
}
}