Added unique lock mutexes to KeyboardManagerState (dev/keyboardManager) (#1789)

* Added unique lock mutexes for thread safety

* Fixed a bug in detect key logic

* Added early unlock statements to fix issue with shortcut guide

* Added comments for unlocks before SendInput and changed some unique_locks to lock_guards
This commit is contained in:
Arjun Balgovind
2020-04-03 10:57:46 -07:00
committed by Udit Singh
parent 467cf919be
commit ac26818005
6 changed files with 134 additions and 25 deletions

View File

@@ -150,7 +150,12 @@ void createEditKeyboardWindow(HINSTANCE hInst, KeyboardManagerState& keyboardMan
{
DWORD originalKey = std::stoi(originalKeyString.c_str());
DWORD newKey = std::stoi(newKeyString.c_str());
keyboardManagerState.AddSingleKeyRemap(originalKey, newKey);
bool result = keyboardManagerState.AddSingleKeyRemap(originalKey, newKey);
if (!result)
{
isSuccess = false;
}
}
else
{
@@ -181,10 +186,12 @@ void createEditKeyboardWindow(HINSTANCE hInst, KeyboardManagerState& keyboardMan
SingleKeyRemapControl::keyboardManagerState = &keyboardManagerState;
// Load existing remaps into UI
std::unique_lock<std::mutex> lock(keyboardManagerState.singleKeyReMap_mutex);
for (const auto& it : keyboardManagerState.singleKeyReMap)
{
SingleKeyRemapControl::AddNewControlKeyRemapRow(keyRemapTable, it.first, it.second);
}
lock.unlock();
// Add remap key button
Windows::UI::Xaml::Controls::Button addRemapKey;

View File

@@ -109,7 +109,7 @@ void createEditShortcutsWindow(HINSTANCE hInst, KeyboardManagerState& keyboardMa
originalShortcutHeader.Margin({ 0, 0, 0, 10 });
tableHeaderRow.Children().Append(originalShortcutHeader);
// Second header textblock in the header row of the shortcut table
// Second header textblock in the header row of the shortcut table
TextBlock newShortcutHeader;
newShortcutHeader.Text(winrt::to_hstring("New Shortcut:"));
newShortcutHeader.FontWeight(Text::FontWeights::Bold());
@@ -143,7 +143,11 @@ void createEditShortcutsWindow(HINSTANCE hInst, KeyboardManagerState& keyboardMa
// Shortcut should consist of atleast two keys
if (originalKeys.size() > 1 && newKeys.size() > 1)
{
keyboardManagerState.AddOSLevelShortcut(originalKeys, newKeys);
bool result = keyboardManagerState.AddOSLevelShortcut(originalKeys, newKeys);
if (!result)
{
isSuccess = false;
}
}
else
{
@@ -179,10 +183,12 @@ void createEditShortcutsWindow(HINSTANCE hInst, KeyboardManagerState& keyboardMa
ShortcutControl::keyboardManagerState = &keyboardManagerState;
// Load existing shortcuts into UI
for (const auto& it: keyboardManagerState.osLevelShortcutReMap)
std::unique_lock<std::mutex> lock(keyboardManagerState.osLevelShortcutReMap_mutex);
for (const auto& it : keyboardManagerState.osLevelShortcutReMap)
{
ShortcutControl::AddNewShortcutControlRow(shortcutTable, it.first, it.second.first);
}
lock.unlock();
// Add shortcut button
Windows::UI::Xaml::Controls::Button addShortcut;

View File

@@ -93,10 +93,8 @@ void createMainWindow(HINSTANCE hInstance, KeyboardManagerState& keyboardManager
Windows::UI::Xaml::Controls::Button bt;
bt.Content(winrt::box_value(winrt::to_hstring("Edit Keyboard")));
bt.Click([&](IInspectable const& sender, RoutedEventArgs const&) {
//keyboardManagerState.SetUIState(KeyboardManagerUIState::DetectKeyWindowActivated);
std::thread th(createEditKeyboardWindow, _hInstance, std::ref(keyboardManagerState));
th.join();
//keyboardManagerState.ResetUIState();
});
Windows::UI::Xaml::Controls::Button bt2;