2020-03-24 01:44:02 +08:00
# include "pch.h"
# include "KeyboardManagerState.h"
// Constructor
KeyboardManagerState : : KeyboardManagerState ( ) :
2020-04-09 05:31:31 +08:00
uiState ( KeyboardManagerUIState : : Deactivated ) , currentUIWindow ( nullptr ) , currentShortcutUI ( nullptr ) , currentSingleKeyUI ( nullptr ) , detectedRemapKey ( NULL )
2020-03-24 01:44:02 +08:00
{
2020-04-20 23:22:36 +08:00
configFile_mutex = CreateMutex (
NULL , // default security descriptor
FALSE , // mutex not owned
KeyboardManagerConstants : : ConfigFileMutexName . c_str ( ) ) ;
}
// Destructor
KeyboardManagerState : : ~ KeyboardManagerState ( )
{
if ( configFile_mutex )
{
CloseHandle ( configFile_mutex ) ;
}
2020-03-24 01:44:02 +08:00
}
2020-05-05 06:49:37 +08:00
// Function to check the if the UI state matches the argument state. For states with detect windows it also checks if the window is in focus.
2020-03-24 01:44:02 +08:00
bool KeyboardManagerState : : CheckUIState ( KeyboardManagerUIState state )
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( uiState_mutex ) ;
2020-03-24 01:44:02 +08:00
if ( uiState = = state )
{
2020-04-04 01:57:46 +08:00
std : : unique_lock < std : : mutex > lock ( currentUIWindow_mutex ) ;
2020-05-05 06:49:37 +08:00
if ( uiState = = KeyboardManagerUIState : : Deactivated | | uiState = = KeyboardManagerUIState : : EditKeyboardWindowActivated | | uiState = = KeyboardManagerUIState : : EditShortcutsWindowActivated )
2020-03-24 01:44:02 +08:00
{
return true ;
}
2020-05-05 06:49:37 +08:00
// If the UI state is a detect window then we also have to ensure that the UI window is in focus.
2020-03-24 01:44:02 +08:00
// GetForegroundWindow can be used here since we only need to check the main parent window and not the sub windows within the content dialog. Using GUIThreadInfo will give more specific sub-windows within the XAML window which is not needed.
else if ( currentUIWindow = = GetForegroundWindow ( ) )
{
return true ;
}
}
return false ;
}
// Function to set the window handle of the current UI window that is activated
void KeyboardManagerState : : SetCurrentUIWindow ( HWND windowHandle )
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( currentUIWindow_mutex ) ;
2020-03-24 01:44:02 +08:00
currentUIWindow = windowHandle ;
}
// Function to set the UI state. When a window is activated, the handle to the window can be passed in the windowHandle argument.
void KeyboardManagerState : : SetUIState ( KeyboardManagerUIState state , HWND windowHandle )
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( uiState_mutex ) ;
2020-03-24 01:44:02 +08:00
uiState = state ;
2020-04-04 01:57:46 +08:00
SetCurrentUIWindow ( windowHandle ) ;
2020-03-24 01:44:02 +08:00
}
// Function to reset the UI state members
void KeyboardManagerState : : ResetUIState ( )
{
SetUIState ( KeyboardManagerUIState : : Deactivated ) ;
2020-04-04 01:57:46 +08:00
// Reset the shortcut UI stored variables
2020-04-09 05:31:31 +08:00
std : : unique_lock < std : : mutex > currentShortcutUI_lock ( currentShortcutUI_mutex ) ;
currentShortcutUI = nullptr ;
currentShortcutUI_lock . unlock ( ) ;
2020-04-04 01:57:46 +08:00
std : : unique_lock < std : : mutex > detectedShortcut_lock ( detectedShortcut_mutex ) ;
2020-04-09 00:11:58 +08:00
detectedShortcut . Reset ( ) ;
2020-04-04 01:57:46 +08:00
detectedShortcut_lock . unlock ( ) ;
2020-03-27 23:38:58 +08:00
2020-04-10 00:20:19 +08:00
std : : unique_lock < std : : mutex > currentShortcut_lock ( currentShortcut_mutex ) ;
currentShortcut . Reset ( ) ;
currentShortcut_lock . unlock ( ) ;
2020-04-04 01:57:46 +08:00
// Reset all the single key remap UI stored variables.
2020-04-09 05:31:31 +08:00
std : : unique_lock < std : : mutex > currentSingleKeyUI_lock ( currentSingleKeyUI_mutex ) ;
currentSingleKeyUI = nullptr ;
currentSingleKeyUI_lock . unlock ( ) ;
2020-04-04 01:57:46 +08:00
std : : unique_lock < std : : mutex > detectedRemapKey_lock ( detectedRemapKey_mutex ) ;
2020-03-27 23:38:58 +08:00
detectedRemapKey = NULL ;
2020-04-04 01:57:46 +08:00
detectedRemapKey_lock . unlock ( ) ;
2020-03-24 01:44:02 +08:00
}
// Function to clear the OS Level shortcut remapping table
void KeyboardManagerState : : ClearOSLevelShortcuts ( )
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( osLevelShortcutReMap_mutex ) ;
2020-03-24 01:44:02 +08:00
osLevelShortcutReMap . clear ( ) ;
}
2020-03-27 23:38:58 +08:00
// Function to clear the Keys remapping table.
void KeyboardManagerState : : ClearSingleKeyRemaps ( )
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( singleKeyReMap_mutex ) ;
2020-03-27 23:38:58 +08:00
singleKeyReMap . clear ( ) ;
}
2020-03-24 01:44:02 +08:00
// Function to add a new OS level shortcut remapping
2020-04-09 00:11:58 +08:00
bool KeyboardManagerState : : AddOSLevelShortcut ( const Shortcut & originalSC , const Shortcut & newSC )
2020-03-24 01:44:02 +08:00
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( osLevelShortcutReMap_mutex ) ;
// Check if the shortcut is already remapped
auto it = osLevelShortcutReMap . find ( originalSC ) ;
if ( it ! = osLevelShortcutReMap . end ( ) )
{
return false ;
}
2020-04-09 00:11:58 +08:00
osLevelShortcutReMap [ originalSC ] = RemapShortcut ( newSC ) ;
2020-04-04 01:57:46 +08:00
return true ;
2020-03-24 01:44:02 +08:00
}
2020-03-27 23:38:58 +08:00
// Function to add a new OS level shortcut remapping
2020-04-09 00:11:58 +08:00
bool KeyboardManagerState : : AddSingleKeyRemap ( const DWORD & originalKey , const DWORD & newRemapKey )
2020-03-27 23:38:58 +08:00
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( singleKeyReMap_mutex ) ;
// Check if the key is already remapped
auto it = singleKeyReMap . find ( originalKey ) ;
if ( it ! = singleKeyReMap . end ( ) )
{
return false ;
}
2020-03-27 23:38:58 +08:00
singleKeyReMap [ originalKey ] = newRemapKey ;
2020-04-04 01:57:46 +08:00
return true ;
2020-03-27 23:38:58 +08:00
}
2020-03-24 01:44:02 +08:00
// Function to set the textblock of the detect shortcut UI so that it can be accessed by the hook
2020-04-09 05:31:31 +08:00
void KeyboardManagerState : : ConfigureDetectShortcutUI ( const StackPanel & textBlock )
2020-03-24 01:44:02 +08:00
{
2020-04-09 05:31:31 +08:00
std : : lock_guard < std : : mutex > lock ( currentShortcutUI_mutex ) ;
currentShortcutUI = textBlock ;
2020-03-24 01:44:02 +08:00
}
2020-03-27 23:38:58 +08:00
// Function to set the textblock of the detect remap key UI so that it can be accessed by the hook
2020-04-09 05:31:31 +08:00
void KeyboardManagerState : : ConfigureDetectSingleKeyRemapUI ( const StackPanel & textBlock )
2020-03-27 23:38:58 +08:00
{
2020-04-09 05:31:31 +08:00
std : : lock_guard < std : : mutex > lock ( currentSingleKeyUI_mutex ) ;
currentSingleKeyUI = textBlock ;
}
void KeyboardManagerState : : AddKeyToLayout ( const StackPanel & panel , const hstring & key )
{
// Textblock to display the detected key
TextBlock remapKey ;
Border border ;
border . Padding ( { 20 , 10 , 20 , 10 } ) ;
2020-04-17 00:16:48 +08:00
border . Margin ( { 0 , 0 , 10 , 0 } ) ;
2020-04-22 04:42:06 +08:00
// Use the base low brush to be consistent with the theme
border . Background ( Windows : : UI : : Xaml : : Application : : Current ( ) . Resources ( ) . Lookup ( box_value ( L " SystemControlBackgroundBaseLowBrush " ) ) . as < Windows : : UI : : Xaml : : Media : : SolidColorBrush > ( ) ) ;
2020-04-09 05:31:31 +08:00
remapKey . FontSize ( 20 ) ;
border . HorizontalAlignment ( HorizontalAlignment : : Left ) ;
border . Child ( remapKey ) ;
remapKey . Text ( key ) ;
panel . Children ( ) . Append ( border ) ;
2020-03-27 23:38:58 +08:00
}
2020-03-24 01:44:02 +08:00
// Function to update the detect shortcut UI based on the entered keys
void KeyboardManagerState : : UpdateDetectShortcutUI ( )
{
2020-04-09 05:31:31 +08:00
std : : lock_guard < std : : mutex > currentShortcutUI_lock ( currentShortcutUI_mutex ) ;
if ( currentShortcutUI = = nullptr )
2020-03-24 01:44:02 +08:00
{
return ;
}
2020-04-04 01:57:46 +08:00
std : : unique_lock < std : : mutex > detectedShortcut_lock ( detectedShortcut_mutex ) ;
2020-04-10 00:20:19 +08:00
std : : unique_lock < std : : mutex > currentShortcut_lock ( currentShortcut_mutex ) ;
// Save the latest displayed shortcut
currentShortcut = detectedShortcut ;
2020-04-17 00:16:48 +08:00
auto detectedShortcutCopy = detectedShortcut ;
2020-04-10 00:20:19 +08:00
currentShortcut_lock . unlock ( ) ;
2020-04-04 01:57:46 +08:00
detectedShortcut_lock . unlock ( ) ;
2020-03-24 01:44:02 +08:00
// Since this function is invoked from the back-end thread, in order to update the UI the dispatcher must be used.
2020-04-17 00:16:48 +08:00
currentShortcutUI . Dispatcher ( ) . RunAsync ( Windows : : UI : : Core : : CoreDispatcherPriority : : Normal , [ this , detectedShortcutCopy ] ( ) {
std : : vector < hstring > shortcut = detectedShortcutCopy . GetKeyVector ( keyboardMap ) ;
2020-04-09 05:31:31 +08:00
currentShortcutUI . Children ( ) . Clear ( ) ;
for ( auto & key : shortcut )
{
AddKeyToLayout ( currentShortcutUI , key ) ;
}
currentShortcutUI . UpdateLayout ( ) ;
2020-03-24 01:44:02 +08:00
} ) ;
}
2020-03-27 23:38:58 +08:00
// Function to update the detect remap key UI based on the entered key.
void KeyboardManagerState : : UpdateDetectSingleKeyRemapUI ( )
{
2020-04-09 05:31:31 +08:00
std : : lock_guard < std : : mutex > currentSingleKeyUI_lock ( currentSingleKeyUI_mutex ) ;
if ( currentSingleKeyUI = = nullptr )
2020-03-27 23:38:58 +08:00
{
return ;
}
// Since this function is invoked from the back-end thread, in order to update the UI the dispatcher must be used.
2020-04-09 09:14:22 +08:00
currentSingleKeyUI . Dispatcher ( ) . RunAsync ( Windows : : UI : : Core : : CoreDispatcherPriority : : Normal , [ this ] ( ) {
2020-04-09 05:31:31 +08:00
currentSingleKeyUI . Children ( ) . Clear ( ) ;
2020-04-09 09:14:22 +08:00
hstring key = winrt : : to_hstring ( keyboardMap . GetKeyName ( detectedRemapKey ) . c_str ( ) ) ;
2020-04-09 05:31:31 +08:00
AddKeyToLayout ( currentSingleKeyUI , key ) ;
currentSingleKeyUI . UpdateLayout ( ) ;
2020-03-27 23:38:58 +08:00
} ) ;
}
2020-03-24 01:44:02 +08:00
// Function to return the currently detected shortcut which is displayed on the UI
2020-04-09 00:11:58 +08:00
Shortcut KeyboardManagerState : : GetDetectedShortcut ( )
2020-03-24 01:44:02 +08:00
{
2020-04-10 00:20:19 +08:00
std : : lock_guard < std : : mutex > lock ( currentShortcut_mutex ) ;
return currentShortcut ;
2020-03-24 01:44:02 +08:00
}
2020-03-27 23:38:58 +08:00
// Function to return the currently detected remap key which is displayed on the UI
2020-04-04 01:57:46 +08:00
DWORD KeyboardManagerState : : GetDetectedSingleRemapKey ( )
2020-03-27 23:38:58 +08:00
{
2020-04-10 00:20:19 +08:00
std : : lock_guard < std : : mutex > lock ( detectedRemapKey_mutex ) ;
return detectedRemapKey ;
2020-03-27 23:38:58 +08:00
}
2020-04-17 00:16:48 +08:00
void KeyboardManagerState : : SelectDetectedRemapKey ( DWORD key )
{
std : : lock_guard < std : : mutex > guard ( detectedRemapKey_mutex ) ;
detectedRemapKey = key ;
UpdateDetectSingleKeyRemapUI ( ) ;
return ;
}
void KeyboardManagerState : : SelectDetectedShortcut ( DWORD key )
{
// Set the new key and store if a change occured
std : : unique_lock < std : : mutex > lock ( detectedShortcut_mutex ) ;
bool updateUI = detectedShortcut . SetKey ( key ) ;
lock . unlock ( ) ;
if ( updateUI )
{
// Update the UI. This function is called here because it should store the set of keys pressed till the last key which was pressed down.
UpdateDetectShortcutUI ( ) ;
}
return ;
}
void KeyboardManagerState : : ResetDetectedShortcutKey ( DWORD key )
{
std : : lock_guard < std : : mutex > lock ( detectedShortcut_mutex ) ;
detectedShortcut . ResetKey ( key ) ;
}
2020-03-24 01:44:02 +08:00
// Function which can be used in HandleKeyboardHookEvent before the single key remap event to use the UI and suppress events while the remap window is active.
2020-05-05 06:49:37 +08:00
KeyboardManagerHelper : : KeyboardHookDecision KeyboardManagerState : : DetectSingleRemapKeyUIBackend ( LowlevelKeyboardEvent * data )
2020-03-24 01:44:02 +08:00
{
// Check if the detect key UI window has been activated
2020-03-27 23:38:58 +08:00
if ( CheckUIState ( KeyboardManagerUIState : : DetectSingleKeyRemapWindowActivated ) )
2020-03-24 01:44:02 +08:00
{
2020-04-17 00:16:48 +08:00
if ( HandleKeyDelayEvent ( data ) )
{
2020-05-05 06:49:37 +08:00
return KeyboardManagerHelper : : KeyboardHookDecision : : Suppress ;
2020-04-17 00:16:48 +08:00
}
2020-04-04 01:57:46 +08:00
// detect the key if it is pressed down
if ( data - > wParam = = WM_KEYDOWN | | data - > wParam = = WM_SYSKEYDOWN )
{
2020-04-17 00:16:48 +08:00
SelectDetectedRemapKey ( data - > lParam - > vkCode ) ;
2020-04-04 01:57:46 +08:00
}
2020-03-24 01:44:02 +08:00
// Suppress the keyboard event
2020-05-05 06:49:37 +08:00
return KeyboardManagerHelper : : KeyboardHookDecision : : Suppress ;
2020-03-24 01:44:02 +08:00
}
2020-05-05 06:49:37 +08:00
// If the settings window is up, remappings should not be applied, but we should not suppress events in the hook
else if ( CheckUIState ( KeyboardManagerUIState : : EditKeyboardWindowActivated ) )
{
return KeyboardManagerHelper : : KeyboardHookDecision : : SkipHook ;
}
return KeyboardManagerHelper : : KeyboardHookDecision : : ContinueExec ;
2020-03-24 01:44:02 +08:00
}
// Function which can be used in HandleKeyboardHookEvent before the os level shortcut remap event to use the UI and suppress events while the remap window is active.
2020-05-05 06:49:37 +08:00
KeyboardManagerHelper : : KeyboardHookDecision KeyboardManagerState : : DetectShortcutUIBackend ( LowlevelKeyboardEvent * data )
2020-03-24 01:44:02 +08:00
{
// Check if the detect shortcut UI window has been activated
if ( CheckUIState ( KeyboardManagerUIState : : DetectShortcutWindowActivated ) )
{
2020-04-17 00:16:48 +08:00
if ( HandleKeyDelayEvent ( data ) )
{
2020-05-05 06:49:37 +08:00
return KeyboardManagerHelper : : KeyboardHookDecision : : Suppress ;
2020-04-17 00:16:48 +08:00
}
2020-03-24 01:44:02 +08:00
// Add the key if it is pressed down
if ( data - > wParam = = WM_KEYDOWN | | data - > wParam = = WM_SYSKEYDOWN )
{
2020-04-17 00:16:48 +08:00
SelectDetectedShortcut ( data - > lParam - > vkCode ) ;
2020-03-24 01:44:02 +08:00
}
// Remove the key if it has been released
else if ( data - > wParam = = WM_KEYUP | | data - > wParam = = WM_SYSKEYUP )
{
2020-04-17 00:16:48 +08:00
ResetDetectedShortcutKey ( data - > lParam - > vkCode ) ;
2020-03-24 01:44:02 +08:00
}
// Suppress the keyboard event
2020-05-05 06:49:37 +08:00
return KeyboardManagerHelper : : KeyboardHookDecision : : Suppress ;
2020-03-24 01:44:02 +08:00
}
// If the detect shortcut UI window is not activated, then clear the shortcut buffer if it isn't empty
2020-04-04 01:57:46 +08:00
else
2020-03-24 01:44:02 +08:00
{
2020-04-04 01:57:46 +08:00
std : : lock_guard < std : : mutex > lock ( detectedShortcut_mutex ) ;
2020-04-09 00:11:58 +08:00
if ( ! detectedShortcut . IsEmpty ( ) )
2020-04-04 01:57:46 +08:00
{
2020-04-09 00:11:58 +08:00
detectedShortcut . Reset ( ) ;
2020-04-04 01:57:46 +08:00
}
2020-03-24 01:44:02 +08:00
}
2020-05-05 06:49:37 +08:00
// If the settings window is up, shortcut remappings should not be applied, but we should not suppress events in the hook
if ( CheckUIState ( KeyboardManagerUIState : : EditShortcutsWindowActivated ) )
{
return KeyboardManagerHelper : : KeyboardHookDecision : : SkipHook ;
}
return KeyboardManagerHelper : : KeyboardHookDecision : : ContinueExec ;
2020-04-17 00:16:48 +08:00
}
void KeyboardManagerState : : RegisterKeyDelay (
DWORD key ,
std : : function < void ( DWORD ) > onShortPress ,
std : : function < void ( DWORD ) > onLongPressDetected ,
std : : function < void ( DWORD ) > onLongPressReleased )
{
std : : lock_guard l ( keyDelays_mutex ) ;
if ( keyDelays . find ( key ) ! = keyDelays . end ( ) )
{
throw std : : invalid_argument ( " This key was already registered. " ) ;
}
keyDelays [ key ] = std : : make_unique < KeyDelay > ( key , onShortPress , onLongPressDetected , onLongPressReleased ) ;
}
void KeyboardManagerState : : UnregisterKeyDelay ( DWORD key )
{
std : : lock_guard l ( keyDelays_mutex ) ;
auto deleted = keyDelays . erase ( key ) ;
if ( deleted = = 0 )
{
throw std : : invalid_argument ( " The key was not previously registered. " ) ;
}
}
bool KeyboardManagerState : : HandleKeyDelayEvent ( LowlevelKeyboardEvent * ev )
{
if ( currentUIWindow ! = GetForegroundWindow ( ) )
{
return false ;
}
std : : lock_guard l ( keyDelays_mutex ) ;
if ( keyDelays . find ( ev - > lParam - > vkCode ) = = keyDelays . end ( ) )
{
return false ;
}
keyDelays [ ev - > lParam - > vkCode ] - > KeyEvent ( ev ) ;
return true ;
2020-04-20 23:22:36 +08:00
}
// Save the updated configuration.
bool KeyboardManagerState : : SaveConfigToFile ( )
{
bool result = true ;
json : : JsonObject configJson ;
json : : JsonObject remapShortcuts ;
json : : JsonObject remapKeys ;
json : : JsonArray inProcessRemapKeysArray ;
json : : JsonArray globalRemapShortcutsArray ;
std : : unique_lock < std : : mutex > lockSingleKeyReMap ( singleKeyReMap_mutex ) ;
for ( const auto & it : singleKeyReMap )
{
json : : JsonObject keys ;
keys . SetNamedValue ( KeyboardManagerConstants : : OriginalKeysSettingName , json : : value ( winrt : : to_hstring ( ( unsigned int ) it . first ) ) ) ;
keys . SetNamedValue ( KeyboardManagerConstants : : NewRemapKeysSettingName , json : : value ( winrt : : to_hstring ( ( unsigned int ) it . second ) ) ) ;
inProcessRemapKeysArray . Append ( keys ) ;
}
lockSingleKeyReMap . unlock ( ) ;
std : : unique_lock < std : : mutex > lockOsLevelShortcutReMap ( osLevelShortcutReMap_mutex ) ;
for ( const auto & it : osLevelShortcutReMap )
{
json : : JsonObject keys ;
keys . SetNamedValue ( KeyboardManagerConstants : : OriginalKeysSettingName , json : : value ( it . first . ToHstringVK ( ) ) ) ;
keys . SetNamedValue ( KeyboardManagerConstants : : NewRemapKeysSettingName , json : : value ( it . second . targetShortcut . ToHstringVK ( ) ) ) ;
globalRemapShortcutsArray . Append ( keys ) ;
}
lockOsLevelShortcutReMap . unlock ( ) ;
remapShortcuts . SetNamedValue ( KeyboardManagerConstants : : GlobalRemapShortcutsSettingName , globalRemapShortcutsArray ) ;
remapKeys . SetNamedValue ( KeyboardManagerConstants : : InProcessRemapKeysSettingName , inProcessRemapKeysArray ) ;
configJson . SetNamedValue ( KeyboardManagerConstants : : RemapKeysSettingName , remapKeys ) ;
configJson . SetNamedValue ( KeyboardManagerConstants : : RemapShortcutsSettingName , remapShortcuts ) ;
// Set timeout of 1sec to wait for file to get free.
DWORD timeout = 1000 ;
auto dwWaitResult = WaitForSingleObject (
configFile_mutex ,
timeout ) ;
if ( dwWaitResult = = WAIT_OBJECT_0 )
{
try
{
json : : to_file ( ( PTSettingsHelper : : get_module_save_folder_location ( KeyboardManagerConstants : : ModuleName ) + L " \\ " + GetCurrentConfigName ( ) + L " .json " ) , configJson ) ;
}
catch ( . . . )
{
result = false ;
}
// Make sure to release the Mutex.
ReleaseMutex ( configFile_mutex ) ;
}
else
{
result = false ;
}
return result ;
}
void KeyboardManagerState : : SetCurrentConfigName ( const std : : wstring & configName )
{
std : : lock_guard < std : : mutex > lock ( currentConfig_mutex ) ;
currentConfig = configName ;
}
std : : wstring KeyboardManagerState : : GetCurrentConfigName ( )
{
std : : lock_guard < std : : mutex > lock ( currentConfig_mutex ) ;
return currentConfig ;
2020-03-24 01:44:02 +08:00
}