OMOTE/Platformio/HAL/Targets/Simulator/KeyPressSim.cpp

47 lines
1.3 KiB
C++

#include "KeyPressSim.hpp"
#include "SimulatorSPSCQueue.hpp"
#include <memory>
KeyPressSim::KeyPressSim()
: mKeyGrabberThread([this] {
while (true) {
HandleKeyPresses();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}),
mKeyHandlerThread([this] {
while (true) {
GrabKeys();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}){};
void KeyPressSim::GrabKeys() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) {
auto keyEventType = event.type == SDL_KEYDOWN ? KeyEvent::Type::Press
: KeyEvent::Type::Release;
const auto SDLK_key = event.key.keysym.sym;
if (KeyMap.count(SDLK_key) > 0) {
QueueKeyEvent(KeyEvent(KeyMap.at(SDLK_key), keyEventType));
}
}
}
};
void KeyPressSim::HandleKeyPresses() {
std::lock_guard lock(mQueueGaurd);
while (!mKeyEventQueue.empty()) {
if (mKeyEventHandler) {
mKeyEventHandler(mKeyEventQueue.front());
}
mKeyEventQueue.pop();
}
};
void KeyPressSim::QueueKeyEvent(KeyEvent aJustOccuredKeyEvent) {
std::lock_guard lock(mQueueGaurd);
mKeyEventQueue.push(aJustOccuredKeyEvent);
};