Skip to content

Commit d17e36b

Browse files
committed
Some code cleanup, removed 'skipifsilent' to prep for chocolatey
1 parent dcb491f commit d17e36b

File tree

5 files changed

+84
-81
lines changed

5 files changed

+84
-81
lines changed

monitor/ActionMonitor/HookWnd.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ HookWnd::HookWnd( IApplication& application, IDisplay& display, IActions& action
77
_virtualMachines(virtualMachines),
88
_actions(actions),
99
_display(display),
10-
_keyState(ACTION_NONE),
11-
_application( application )
10+
_application( application ),
11+
_keyState(ACTION_NONE)
1212
{
1313
}
1414

@@ -37,7 +37,6 @@ bool HookWnd::IsSpecialKey(const WPARAM wParam)
3737
return (wParam == SPECIAL_KEY);
3838
}
3939

40-
4140
LRESULT HookWnd::OnMessage(const UINT msg, const WPARAM wParam, const LPARAM lParam)
4241
{
4342
if( msg == WM_CLOSE )
@@ -121,8 +120,8 @@ LRESULT HookWnd::OnHookKeyDown(WPARAM wParam, LPARAM lParam)
121120
TRACE("KeyDown 0x%x\n", wParam);
122121

123122
/**
124-
* The WM_KEYDOWN message is posted to the window with the keyboard focus when a nonsystem key is pressed.
125-
* A nonsystem key is a key that is pressed when the ALT key is not pressed.
123+
* The WM_KEYDOWN message is posted to the window with the keyboard focus when a non-system key is pressed.
124+
* A non-system key is a key that is pressed when the ALT key is not pressed.
126125
*/
127126

128127
// if it is the special key then tell the system that from now own
@@ -200,7 +199,7 @@ LRESULT HookWnd::OnHookKeyDown(WPARAM wParam, LPARAM lParam)
200199
memset(ks, 0, sizeof(ks));
201200
GetKeyboardState(ks);
202201
WORD w;
203-
UINT scan = 0;
202+
const UINT scan = 0;
204203

205204
ks[VK_SHIFT] = 1;
206205
if ((_keyState & ACTION_SHIFT_DOWN) == ACTION_SHIFT_DOWN)
@@ -273,8 +272,8 @@ LRESULT HookWnd::OnHookKeyUp(WPARAM wParam, LPARAM lParam)
273272
}
274273

275274
/**
276-
* The WM_KEYUP message is posted to the window with the keyboard focus when a nonsystem key is released.
277-
* A nonsystem key is a key that is pressed when the ALT key is not pressed,
275+
* The WM_KEYUP message is posted to the window with the keyboard focus when a non-system key is released.
276+
* A non-system key is a key that is pressed when the ALT key is not pressed,
278277
* or a keyboard key that is pressed when a window has the keyboard focus.
279278
*/
280279
return 0L;

monitor/ActionMonitor/HookWnd.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "IApplication.h"
2020
#include "../api/IVirtualMachines.h"
2121
#include "threads/workers.h"
22-
#include "ActiveActionsRunner.h"
2322

2423
#define ACTION_NONE 0x000
2524
#define ACTION_MAINKEY_DOWN 0x001
@@ -38,7 +37,7 @@ class HookWnd final : public CommonWnd, protected myodd::threads::Workers
3837
bool Create() override;
3938

4039
protected:
41-
LRESULT OnHookKeyChar(WPARAM wParam, LPARAM lParam);
40+
static LRESULT OnHookKeyChar(WPARAM wParam, LPARAM lParam);
4241
LRESULT OnHookKeyDown(WPARAM wParam, LPARAM lParam);
4342
LRESULT OnHookKeyUp(WPARAM wParam, LPARAM lParam);
4443
LRESULT OnMessage(UINT msg, WPARAM wParam, LPARAM lParam) override;
@@ -54,7 +53,7 @@ class HookWnd final : public CommonWnd, protected myodd::threads::Workers
5453
IActions& _actions;
5554

5655
/**
57-
* \brief the class that will recieve show/hide requests.
56+
* \brief the class that will receive show/hide requests.
5857
*/
5958
IDisplay& _display;
6059

@@ -67,5 +66,5 @@ class HookWnd final : public CommonWnd, protected myodd::threads::Workers
6766
unsigned long _keyState;
6867

6968
static bool IsSpecialKeyDown();
70-
static bool IsSpecialKey(const WPARAM wParam);
69+
static bool IsSpecialKey( WPARAM wParam);
7170
};

monitor/hook/KeyboardMonitor.cpp

+60-55
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,48 @@
55

66
KeyboardMonitor* keyboardMonitor;
77

8-
KeyboardMonitor::KeyboardMonitor( HANDLE hModule ) :
8+
KeyboardMonitor::KeyboardMonitor(const HANDLE hModule ) :
99
m_hModule( hModule ),
10-
m_hhook( NULL ),
11-
m_bRejectKeyBoardInputs( FALSE )
10+
m_bRejectKeyBoardInputs(false),
11+
m_hhook(nullptr)
1212
{
1313
UWM_KEYBOARD_CHAR = RegisterWindowMessage(UWM_KEYBOARD_MSG_CHAR);
1414
UWM_KEYBOARD_UP = RegisterWindowMessage(UWM_KEYBOARD_MSG_UP);
1515
UWM_KEYBOARD_DOWN = RegisterWindowMessage(UWM_KEYBOARD_MSG_DOWN);
1616
}
1717

18-
KeyboardMonitor::~KeyboardMonitor(void)
18+
KeyboardMonitor::~KeyboardMonitor()
1919
{
2020
ClearHooks();
2121
}
2222

2323
// -----------------------------------------------------------------------
24-
BOOL KeyboardMonitor::HandleHook (int nCode )
24+
bool KeyboardMonitor::HandleHook ( const int nCode )
2525
{
2626
if(nCode < 0 || HC_ACTION != nCode )
2727
{
28-
return FALSE;
28+
return false;
2929
}
30-
return TRUE;
30+
return true;
3131
}
3232

3333
// -----------------------------------------------------------------------
3434
LRESULT CALLBACK KeyboardMonitor::CallLowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
3535
{
3636
// are we handling that hook?
37-
if( FALSE == keyboardMonitor->HandleHook( nCode ))
37+
if(false == keyboardMonitor->HandleHook( nCode ))
3838
{
39-
return CallNextHookEx( keyboardMonitor->GetHHOOK(), nCode, wParam, lParam);
39+
return CallNextHookEx( keyboardMonitor->GetHhook(), nCode, wParam, lParam);
4040
}
4141

4242
// By returning a non-zero value from the hook procedure, the
4343
// message does not get passed to the target window
44-
KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;
45-
int error=GetLastError();
44+
const auto *pkbhs = reinterpret_cast<KBDLLHOOKSTRUCT *>(lParam);
4645
switch (nCode)
4746
{
4847
case HC_ACTION:
4948
{
50-
LRESULT hHandle = keyboardMonitor->HandleMessage( wParam, pkbhs->vkCode, pkbhs->dwExtraInfo );
49+
const auto hHandle = keyboardMonitor->HandleMessage( wParam, pkbhs->vkCode, pkbhs->dwExtraInfo );
5150
switch ( hHandle )
5251
{
5352
case 1: // the key was handled by someone
@@ -56,7 +55,7 @@ LRESULT CALLBACK KeyboardMonitor::CallLowLevelKeyboardProc(int nCode, WPARAM wPa
5655
// other processes are supposed to ignore WM_NULL
5756
// this is not full proof, (we might not be the first hook in the queue), but the odds of
5857
// multiple hooks all trying to do the same thing is limited.
59-
if( keyboardMonitor->RejectKeyboadInputs() == TRUE )
58+
if( keyboardMonitor->RejectKeyboadInputs() == true)
6059
{
6160
return 1;
6261
}
@@ -65,6 +64,9 @@ LRESULT CALLBACK KeyboardMonitor::CallLowLevelKeyboardProc(int nCode, WPARAM wPa
6564

6665
case 0:
6766
break;
67+
68+
default:
69+
break;
6870
}
6971
}
7072

@@ -81,24 +83,27 @@ LRESULT CALLBACK KeyboardMonitor::CallLowLevelKeyboardProc(int nCode, WPARAM wPa
8183
default:
8284
break;
8385
}
84-
return CallNextHookEx ( keyboardMonitor->GetHHOOK(), nCode, wParam, lParam);
86+
return CallNextHookEx ( keyboardMonitor->GetHhook(), nCode, wParam, lParam);
8587
}// CallLowLevelKeyboardProc
8688

87-
// -----------------------------------------------------------------------
88-
// Just look if one of the windows expects that key
89-
BOOL KeyboardMonitor::IsSpecialKey( const WPARAM wParam ) const
89+
/**
90+
* \brief check if the given param means that the one of the special key was pressed.
91+
* \param wParam the key we are checking against.
92+
* \return if the given key is a special key or not.
93+
*/
94+
bool KeyboardMonitor::IsSpecialKey( const WPARAM wParam ) const
9095
{
91-
for( std::map< HWND, WPARAM >::const_iterator it = m_mapWindows.begin(); it != m_mapWindows.end(); ++it )
96+
for(auto it = m_mapWindows.begin(); it != m_mapWindows.end(); ++it )
9297
{
9398
if( wParam == it->second )
9499
{
95100
// yes, this window uses that key.
96-
return TRUE;
101+
return true;
97102
}
98103
}
99104

100105
// if we are here we never found it.
101-
return FALSE;
106+
return false;
102107
}
103108

104109
// -----------------------------------------------------------------------
@@ -119,10 +124,10 @@ LRESULT KeyboardMonitor::HandleMessage
119124
}
120125

121126
// get the number of milli secs from start of system
122-
DWORD tick = GetTickCount();
127+
const auto tick = GetTickCount();
123128
//
124129
// do not allow items to be too quick
125-
if( GetLastKey( tick ) .similar( msg, wParam) )
130+
if( GetLastKey( tick ) .Similar( msg, wParam) )
126131
{
127132
// the key we just entered has been repeated too soon for us and is rejected.
128133
// TRACE( "<< Repeat Key >>\n" );
@@ -132,10 +137,10 @@ LRESULT KeyboardMonitor::HandleMessage
132137
// it is not uncommon for apps to request the last key more than once.
133138
SetLastKey( LAST_KEY(msg, wParam, tick) );
134139

135-
LRESULT lResult = 0L; // assume that nobody will handle it.
136-
for( std::map< HWND, WPARAM >::const_iterator it = m_mapWindows.begin(); it != m_mapWindows.end(); ++it )
140+
auto lResult = 0L; // assume that nobody will handle it.
141+
for( auto it = m_mapWindows.begin(); it != m_mapWindows.end(); ++it )
137142
{
138-
HWND hWnd = it->first;
143+
const auto hWnd = it->first;
139144
switch( msg )
140145
{
141146
case WM_KEYDOWN:
@@ -165,9 +170,9 @@ LRESULT KeyboardMonitor::HandleMessage
165170
}
166171

167172
// -----------------------------------------------------------------------
168-
BOOL KeyboardMonitor::RejectKeyboadInputs( BOOL bReject )
173+
bool KeyboardMonitor::RejectKeyboadInputs(bool bReject )
169174
{
170-
BOOL bThen = m_bRejectKeyBoardInputs;
175+
const auto bThen = m_bRejectKeyBoardInputs;
171176
m_bRejectKeyBoardInputs = bReject;
172177
return bThen;
173178
}
@@ -178,7 +183,7 @@ BOOL KeyboardMonitor::RejectKeyboadInputs( BOOL bReject )
178183
*
179184
* @return bool return true if we are rejecting all keyboard inputs.
180185
*/
181-
BOOL KeyboardMonitor::RejectKeyboadInputs( ) const
186+
bool KeyboardMonitor::RejectKeyboadInputs( ) const
182187
{
183188
return m_bRejectKeyBoardInputs;
184189
}
@@ -190,13 +195,13 @@ void KeyboardMonitor::SetLastKey( const LAST_KEY& lk )
190195
}
191196

192197
/**
193-
* if the last DWORD used was more than 100 milli secs ago we can assume that it is a new key
198+
* \brief if the last DWORD used was more than 100 milliseconds ago we can assume that it is a new key
194199
* so we just return an empty key.
195200
*
196-
* @param DWORD the current time so we can compare if the last key was a long time ago,
197-
* @return LAST_KEY the last key that was saved.
201+
* \param dwCurrentMilli the current time so we can compare if the last key was a long time ago,
202+
* \return LAST_KEY the last key that was saved.
198203
*/
199-
KeyboardMonitor::LAST_KEY KeyboardMonitor::GetLastKey( DWORD dwCurrentMilli)
204+
KeyboardMonitor::LAST_KEY KeyboardMonitor::GetLastKey( const DWORD dwCurrentMilli) const
200205
{
201206
if( (dwCurrentMilli - m_lk.tick() ) > 100 /* 100 milli seconds*/)
202207
{
@@ -213,13 +218,13 @@ KeyboardMonitor::LAST_KEY KeyboardMonitor::GetLastKey( DWORD dwCurrentMilli)
213218
}
214219

215220
// -----------------------------------------------------------------------
216-
BOOL KeyboardMonitor::RemoveHwnd( HWND hWnd )
221+
bool KeyboardMonitor::RemoveHwnd( const HWND hWnd )
217222
{
218-
std::map< HWND, WPARAM >::const_iterator iter = m_mapWindows.find( hWnd );
223+
const auto iter = m_mapWindows.find( hWnd );
219224
if( iter == m_mapWindows.end() )
220225
{
221226
// don't know that item
222-
return FALSE;
227+
return false;
223228
}
224229

225230
// remove it
@@ -232,68 +237,68 @@ BOOL KeyboardMonitor::RemoveHwnd( HWND hWnd )
232237
}
233238

234239
// if we are here then we still have some hooks needed.
235-
return TRUE;
240+
return true;
236241
}
237242

238243
// -----------------------------------------------------------------------
239-
BOOL KeyboardMonitor::CreateHooks()
244+
bool KeyboardMonitor::CreateHooks()
240245
{
241246
// have we already created the hooks?
242-
if( NULL != GetHHOOK() )
247+
if(nullptr != GetHhook() )
243248
{
244-
return TRUE;
249+
return true;
245250
}
246251

247252
// hook into the low level keyboard message
248-
HHOOK hookllKey = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)CallLowLevelKeyboardProc, (HINSTANCE)m_hModule,0);
253+
const auto hookllKey = SetWindowsHookEx(WH_KEYBOARD_LL, static_cast<HOOKPROC>(CallLowLevelKeyboardProc), static_cast<HINSTANCE>(m_hModule),0);
249254

250255
// make sure that they are all created properly.
251-
if( hookllKey == NULL )
256+
if( hookllKey == nullptr)
252257
{
253-
return FALSE;
258+
return false;
254259
}
255260

256261
// save the value
257-
SetHHOOK( hookllKey );
262+
SetHhook( hookllKey );
258263

259-
return TRUE;
264+
return true;
260265
}
261266

262267
// -----------------------------------------------------------------------
263-
BOOL KeyboardMonitor::ClearHooks( )
268+
bool KeyboardMonitor::ClearHooks( )
264269
{
265-
if( NULL == GetHHOOK() )
270+
if( nullptr == GetHhook() )
266271
{
267272
// we were not hooked in the first place.
268-
return FALSE;
273+
return false;
269274
}
270275

271276

272-
BOOL bResult = UnhookWindowsHookEx( GetHHOOK() );
277+
const auto bResult = UnhookWindowsHookEx( GetHhook() );
273278

274-
// what ever happened, (True or False), the user no longer wants that hook.
275-
SetHHOOK( NULL );
279+
// what ever happened, (true or False), the user no longer wants that hook.
280+
SetHhook(nullptr);
276281

277282
return bResult;
278283
}
279284

280285
// -----------------------------------------------------------------------
281-
BOOL KeyboardMonitor::AddHwnd( HWND hWnd, WPARAM vKey )
286+
bool KeyboardMonitor::AddHwnd( const HWND hWnd, const WPARAM vKey )
282287
{
283288
if( !CreateHooks() )
284289
{
285-
return FALSE;
290+
return false;
286291
}
287292

288293
// do we aready have that window?
289-
std::map< HWND, WPARAM >::const_iterator iter = m_mapWindows.find( hWnd );
294+
const auto iter = m_mapWindows.find( hWnd );
290295
if( iter != m_mapWindows.end() )
291296
{
292-
return FALSE; // already exists.
297+
return false; // already exists.
293298
}
294299

295300
// this is new so we need to add it here.
296301
m_mapWindows[ hWnd ] = vKey;
297302

298-
return TRUE; // this was added.
303+
return true; // this was added.
299304
}

0 commit comments

Comments
 (0)