Skip to content

Commit b6bc2a7

Browse files
committed
Merge branch 'thumbbehavior'
2 parents 3ee3955 + bbddde8 commit b6bc2a7

24 files changed

+534
-130
lines changed

XMapLib/CPPRunnerGeneric.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ namespace sds
2929
StopThread();
3030
}
3131
protected:
32-
LambdaType m_lambda;
32+
const LambdaType m_lambda;
3333
InternalData m_local_state{}; // default constructed type InternalData
34-
std::atomic<bool> m_is_stop_requested = false;
34+
std::atomic<bool> m_is_stop_requested{ false };
3535
std::unique_ptr<std::thread> m_local_thread;
3636
std::mutex m_state_mutex;
3737
public:

XMapLib/KeyboardKeyMap.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,33 +60,32 @@ namespace sds
6060
const char printed = Utilities::VirtualMap::GetCharFromVK(obj.MappedToVK);
6161
const bool isPrintable = std::isprint(static_cast<unsigned char>(printed));
6262
std::osyncstream ss(os);
63-
ss << "[KeyboardKeyMap]" << std::endl;
64-
ss << "SendingElementVK:" << obj.SendingElementVK << std::endl;
65-
ss << "MappedToVK:" << obj.MappedToVK << std::endl;
66-
if (isPrintable)
67-
ss << "MappedToVK(AKA):" << printed << std::endl;
68-
ss << "UsesRepeat:" << obj.UsesRepeat << std::endl;
69-
ss << "LastAction:" << obj.LastAction << std::endl;
70-
ss << obj.LastSentTime << std::endl;
71-
ss << "[/KeyboardKeyMap]";
63+
ss << "[KeyboardKeyMap]" << " ";
64+
ss << "SendingElementVK:" << obj.SendingElementVK << " ";
65+
ss << "MappedToVK:" << obj.MappedToVK << " ";
66+
ss << "MappedToVK(AKA):" << (isPrintable ? printed : ' ') << " ";
67+
ss << "UsesRepeat:" << obj.UsesRepeat << " ";
68+
ss << "LastAction:" << obj.LastAction << " ";
69+
ss << obj.LastSentTime << " ";
70+
ss << "[/KeyboardKeyMap]" << " ";
7271
return os;
7372
}
7473
/// <summary>
7574
/// Operator<< overload for std::string specialization,
7675
/// writes relevant map details to the std::string.
76+
/// NOTE: Changing this will break the DLL API which depends on this format.
7777
/// </summary>
7878
friend std::string& operator<<(std::string& os, const KeyboardKeyMap& obj)
7979
{
8080
const char printed = Utilities::VirtualMap::GetCharFromVK(obj.MappedToVK);
8181
const bool isPrintable = std::isprint(static_cast<unsigned char>(printed));
8282
std::stringstream ss;
83-
ss << "[KeyboardKeyMap]" << std::endl;
83+
ss << "[KeyboardKeyMap]" << " ";
8484
ss << "SendingElementVK:" << obj.SendingElementVK << " ";
8585
ss << "MappedToVK:" << obj.MappedToVK << " ";
86-
if(isPrintable)
87-
ss << "MappedToVK(AKA):" << printed << std::endl;
88-
ss << "UsesRepeat:" << obj.UsesRepeat << " ";
89-
ss << "[/KeyboardKeyMap]" << std::endl;
86+
ss << "MappedToVK(AKA):" << (isPrintable? printed : ' ') << " ";
87+
ss << "UsesRepeat:" << std::boolalpha << obj.UsesRepeat << " ";
88+
ss << "[/KeyboardKeyMap]" << " ";
9089
os += ss.str();
9190
return os;
9291
}

XMapLib/KeyboardMapper.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,10 @@ namespace sds
9999
while (!stopCondition)
100100
{
101101
const std::vector<XINPUT_KEYSTROKE> states = m_poller.getAndClearStates();
102-
std::for_each(states.begin(), states.end(), [this](auto& cur)
103-
{
104-
m_translator.ProcessKeystroke(cur);
105-
});
106-
//m_translator.ProcessKeystroke(m_poller.GetUpdatedState());
102+
for(const auto &cur: states)
103+
{
104+
m_translator.ProcessKeystroke(cur);
105+
}
107106
std::this_thread::sleep_for(std::chrono::milliseconds(KeyboardSettings::THREAD_DELAY_POLLER));
108107
}
109108
}

XMapLib/KeyboardPlayerInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace sds
1010
{
1111
using PidType = int;
1212
//ISO CPP guidelines C.45 followed here: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-default
13-
std::atomic<PidType> player_id = 0;
13+
std::atomic<PidType> player_id{ 0 };
1414
//default ctor
1515
KeyboardPlayerInfo() = default;
1616
//copy constructor

XMapLib/KeyboardTranslator.h

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ namespace sds
4242
//Key repeat loop
4343
KeyRepeatLoop();
4444
//search the map for a matching virtual key and send it
45-
std::for_each(m_map_token_info.begin(), m_map_token_info.end(), [this, &stroke](auto &w)
45+
for(auto &w: m_map_token_info)
46+
{
47+
if (w.SendingElementVK == stroke.VirtualKey)
4648
{
47-
if (w.SendingElementVK == stroke.VirtualKey)
48-
{
49-
this->Normal(w, stroke);
50-
}
51-
});
49+
this->Normal(w, stroke);
50+
}
51+
}
5252
}
5353
std::string AddKeyMap(KeyboardKeyMap w)
5454
{
@@ -71,25 +71,25 @@ namespace sds
7171
{
7272
//If enough time has passed, reset the key for use again, provided it uses the key-repeat behavior--
7373
//otherwise reset it immediately.
74-
std::for_each(m_map_token_info.begin(), m_map_token_info.end(), [](auto& e)
75-
{
76-
const bool DoUpdate = (e.LastAction == InpType::KEYUP && e.LastSentTime.IsElapsed()) && e.UsesRepeat;
77-
const bool DoImmediate = e.LastAction == InpType::KEYUP && !e.UsesRepeat;
78-
if (DoUpdate || DoImmediate)
79-
e.LastAction = InpType::NONE;
80-
});
74+
for(auto &e: m_map_token_info)
75+
{
76+
const bool DoUpdate = (e.LastAction == InpType::KEYUP && e.LastSentTime.IsElapsed()) && e.UsesRepeat;
77+
const bool DoImmediate = e.LastAction == InpType::KEYUP && !e.UsesRepeat;
78+
if (DoUpdate || DoImmediate)
79+
e.LastAction = InpType::NONE;
80+
}
8181
}
8282
void KeyRepeatLoop()
8383
{
84-
std::for_each(m_map_token_info.begin(), m_map_token_info.end(), [this](auto& w)
84+
for(auto &w: m_map_token_info)
85+
{
86+
using AT = sds::KeyboardKeyMap::ActionType;
87+
if (w.UsesRepeat && (((w.LastAction == AT::KEYDOWN) || (w.LastAction == AT::KEYREPEAT))))
8588
{
86-
using AT = sds::KeyboardKeyMap::ActionType;
87-
if(w.UsesRepeat && (((w.LastAction == AT::KEYDOWN) || (w.LastAction == AT::KEYREPEAT))))
88-
{
89-
if (w.LastSentTime.IsElapsed())
90-
this->SendTheKey(w, true, AT::KEYREPEAT);
91-
}
92-
});
89+
if (w.LastSentTime.IsElapsed())
90+
this->SendTheKey(w, true, AT::KEYREPEAT);
91+
}
92+
}
9393
}
9494
/// <summary>
9595
/// Normal keypress simulation logic.
@@ -128,8 +128,8 @@ namespace sds
128128
bool IsOvertaking(const KeyboardKeyMap &detail, KeyboardKeyMap &outOvertaken)
129129
{
130130
//Is detail a thumbstick direction map, and if so, which thumbstick.
131-
const auto leftAxisIterator = std::find(KeyboardSettings::THUMBSTICK_L_VK_LIST.begin(), KeyboardSettings::THUMBSTICK_L_VK_LIST.end(), detail.SendingElementVK);
132-
const auto rightAxisIterator = std::find(KeyboardSettings::THUMBSTICK_R_VK_LIST.begin(), KeyboardSettings::THUMBSTICK_R_VK_LIST.end(), detail.SendingElementVK);
131+
const auto leftAxisIterator = std::ranges::find(KeyboardSettings::THUMBSTICK_L_VK_LIST, detail.SendingElementVK);
132+
const auto rightAxisIterator = std::ranges::find(KeyboardSettings::THUMBSTICK_R_VK_LIST, detail.SendingElementVK);
133133
const bool leftStick = leftAxisIterator != KeyboardSettings::THUMBSTICK_L_VK_LIST.end();
134134
const bool rightStick = rightAxisIterator != KeyboardSettings::THUMBSTICK_R_VK_LIST.end();
135135
//find a key-down'd or repeat'd direction of the same thumbstick
@@ -140,10 +140,10 @@ namespace sds
140140
auto TestFunc = [&stickSettingList, &detail](const KeyboardKeyMap& elem)
141141
{
142142
if ((elem.LastAction == InpType::KEYDOWN || elem.LastAction == InpType::KEYREPEAT) && elem.SendingElementVK != detail.SendingElementVK)
143-
return std::find(stickSettingList.begin(), stickSettingList.end(), elem.SendingElementVK) != stickSettingList.end();
143+
return std::ranges::find(stickSettingList, elem.SendingElementVK) != stickSettingList.end();
144144
return false;
145145
};
146-
const auto mpit = std::find_if(m_map_token_info.begin(), m_map_token_info.end(), TestFunc);
146+
const auto mpit = std::ranges::find_if(m_map_token_info, TestFunc);
147147
if (mpit == m_map_token_info.end())
148148
return false;
149149
outOvertaken = *mpit;

XMapLib/MouseMapper.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ namespace sds
1717
using InternalType = int;
1818
using LambdaRunnerType = sds::CPPRunnerGeneric<InternalType>;
1919
using lock = LambdaRunnerType::ScopedLockType;
20-
std::atomic<StickMap> m_stickmap_info = StickMap::NEITHER_STICK;
21-
std::atomic<SHORT> m_thread_x = 0;
22-
std::atomic<SHORT> m_thread_y = 0;
23-
std::atomic<int> m_mouse_sensitivity = MouseSettings::SENSITIVITY_DEFAULT;
20+
std::atomic<StickMap> m_stickmap_info{ StickMap::NEITHER_STICK };
21+
std::atomic<SHORT> m_thread_x{ 0 };
22+
std::atomic<SHORT> m_thread_y{0};
23+
std::atomic<int> m_mouse_sensitivity{ MouseSettings::SENSITIVITY_DEFAULT };
2424
sds::MousePlayerInfo m_local_player;
2525
sds::MouseInputPoller m_poller;
2626
std::unique_ptr<LambdaRunnerType> m_workThread;

XMapLib/MouseMoveThread.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ namespace sds
1313
using InternalType = int;
1414
using LambdaRunnerType = sds::CPPRunnerGeneric<InternalType>;
1515
using lock = LambdaRunnerType::ScopedLockType;
16-
std::atomic<size_t> m_x_axis_delay = 1;
17-
std::atomic<size_t> m_y_axis_delay = 1;
18-
std::atomic<bool> m_is_x_moving = false;
19-
std::atomic<bool> m_is_y_moving = false;
20-
std::atomic<bool> m_is_x_positive = false;
21-
std::atomic<bool> m_is_y_positive = false;
16+
std::atomic<size_t> m_x_axis_delay{ 1 };
17+
std::atomic<size_t> m_y_axis_delay{ 1 };
18+
std::atomic<bool> m_is_x_moving{ false };
19+
std::atomic<bool> m_is_y_moving{ false };
20+
std::atomic<bool> m_is_x_positive{ false };
21+
std::atomic<bool> m_is_y_positive{ false };
2222
std::unique_ptr<LambdaRunnerType> m_workThread;
2323
void InitWorkThread() noexcept
2424
{

XMapLib/MousePlayerInfo.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace sds
1010
using PidType = int;
1111
using DzType = int;
1212
//ISO CPP guidelines C.45 followed here: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-default
13-
std::atomic<DzType> left_x_dz = XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE; // left stick X axis dz
14-
std::atomic<DzType> left_y_dz = XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE; // left stick Y axis dz
15-
std::atomic<DzType> right_x_dz = XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE; // right stick X axis dz
16-
std::atomic<DzType> right_y_dz = XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE; // right stick Y axis dz
17-
std::atomic<PidType> player_id = 0;
13+
std::atomic<DzType> left_x_dz{ XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE }; // left stick X axis dz
14+
std::atomic<DzType> left_y_dz{ XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE }; // left stick Y axis dz
15+
std::atomic<DzType> right_x_dz{ XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE }; // right stick X axis dz
16+
std::atomic<DzType> right_y_dz{ XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE }; // right stick Y axis dz
17+
std::atomic<PidType> player_id{ 0 };
1818
//default ctor
1919
MousePlayerInfo() = default;
2020
//copy constructor

XMapLib/ThumbstickToDelay.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace sds
1313
class ThumbstickToDelay
1414
{
1515
const std::string BAD_DELAY_MSG = "Bad timer delay value, exception.";
16-
inline static std::atomic<bool> m_is_deadzone_activated = false; //shared between instances
16+
inline static std::atomic<bool> m_is_deadzone_activated{ false }; //shared between instances
1717
float m_alt_deadzone_multiplier = MouseSettings::ALT_DEADZONE_MULT_DEFAULT;
1818
int m_axis_sensitivity = MouseSettings::SENSITIVITY_DEFAULT;
1919
int m_x_axis_deadzone = MouseSettings::DEADZONE_DEFAULT;

XMapLib/XMapLib.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
#include "MouseMapper.h"
88

99
using namespace std;
10-
void AddTestKeyMappings(sds::KeyboardMapper& mapper);
10+
void AddTestKeyMappings(sds::KeyboardMapper& mapper, std::osyncstream &ss);
1111

1212
class GetterExit
1313
{
1414
std::unique_ptr<std::thread> workerThread;
15-
std::atomic<bool> m_exitState = false;
15+
std::atomic<bool> m_exitState{ false };
1616
const sds::KeyboardMapper& m_mp;
1717
public:
1818
GetterExit(const sds::KeyboardMapper& m) : m_mp(m) { startThread(); }
@@ -36,11 +36,12 @@ class GetterExit
3636
}
3737
void workThread()
3838
{
39-
std::cin.get(); // block and wait
39+
std::cin.get(); // block and wait for enter key
4040
std::cin.clear();
4141
std::osyncstream ss(std::cerr);
42+
//prints out the maps, for debugging info.
4243
auto mapList = m_mp.GetMaps();
43-
for_each(begin(mapList), end(mapList), [&ss](const sds::KeyboardKeyMap& theMap)
44+
ranges::for_each(mapList, [&ss](const sds::KeyboardKeyMap& theMap)
4445
{
4546
ss << theMap << endl << endl;
4647
});
@@ -52,40 +53,43 @@ int main()
5253
{
5354
using namespace sds;
5455
using namespace sds::Utilities;
56+
5557
MousePlayerInfo player;
5658
KeyboardPlayerInfo kplayer;
5759
MouseMapper mouser(player);
5860
KeyboardMapper keyer(kplayer);
59-
AddTestKeyMappings(keyer);
61+
std::osyncstream ss(std::cout);
62+
AddTestKeyMappings(keyer, ss);
6063
GetterExit getter(keyer);
6164
std::string err = mouser.SetSensitivity(75); // 75 out of 100
6265
Utilities::LogError(err); // won't do anything if the string is empty
6366
mouser.SetStick(StickMap::RIGHT_STICK);
6467

65-
std::cout << "[Enter] to dump keymap contents and quit." << endl;
66-
std::cout << "Xbox controller polling started..." << endl;
67-
std::cout << "Controller reported as: " << (mouser.IsControllerConnected() && keyer.IsControllerConnected() ? "Connected.": "Disconnected.") << std::endl;
68+
ss << "[Enter] to dump keymap contents and quit." << endl;
69+
ss << "Xbox controller polling started..." << endl;
70+
ss << "Controller reported as: " << (mouser.IsControllerConnected() && keyer.IsControllerConnected() ? "Connected.": "Disconnected.") << std::endl;
71+
ss.emit();
6872
do
6973
{
7074
const bool isControllerConnected = mouser.IsControllerConnected() && keyer.IsControllerConnected();
7175
const bool isThreadRunning = mouser.IsRunning() && keyer.IsRunning();
7276
if (!isThreadRunning && isControllerConnected)
7377
{
74-
std::cout << "Controller reported as: " << "Connected." << std::endl;
78+
ss << "Controller reported as: " << "Connected." << std::endl;
7579
keyer.Start();
7680
mouser.Start();
7781
}
7882
if ((!isControllerConnected) && isThreadRunning)
7983
{
80-
std::cout << "Controller reported as: " << "Disconnected." << std::endl;
84+
ss << "Controller reported as: " << "Disconnected." << std::endl;
8185
keyer.Stop();
8286
mouser.Stop();
8387
}
8488
std::this_thread::sleep_for(std::chrono::milliseconds(10));
8589
} while (!getter());
8690
return 0;
8791
}
88-
void AddTestKeyMappings(sds::KeyboardMapper& mapper)
92+
void AddTestKeyMappings(sds::KeyboardMapper& mapper, std::osyncstream &ss)
8993
{
9094
using namespace sds;
9195
const auto buttons =
@@ -114,15 +118,16 @@ void AddTestKeyMappings(sds::KeyboardMapper& mapper)
114118
KeyboardKeyMap{VK_PAD_X, 0x52, false} // 'r'
115119
};
116120
std::string errorCondition;
117-
for_each(begin(buttons), end(buttons), [&mapper, &errorCondition](const KeyboardKeyMap& m)
121+
ranges::for_each(buttons, [&mapper, &errorCondition](const KeyboardKeyMap& m)
118122
{
119123
if (errorCondition.empty())
120124
{
121125
errorCondition = mapper.AddMap(m);
122126
}
123127
});
124128
if (!errorCondition.empty())
125-
cout << "Added buttons until error: " << errorCondition << endl;
129+
ss << "Added buttons until error: " << errorCondition << endl;
126130
else
127-
cout << "Added: " << buttons.size() << " key mappings." << endl;
131+
ss << "Added: " << buttons.size() << " key mappings." << endl;
132+
ss.emit();
128133
}

0 commit comments

Comments
 (0)