97 lines
3.1 KiB
C++
97 lines
3.1 KiB
C++
/*
|
|
* Copyright (c) 2023 Fancy Code.
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
#include "KeyManager.h"
|
|
#include "ILog.h"
|
|
std::shared_ptr<KeyManager> &KeyManager::GetInstance(std::shared_ptr<KeyManager> *impl)
|
|
{
|
|
static auto instance = std::make_shared<KeyManager>();
|
|
if (impl) {
|
|
if (instance.use_count() == 1) {
|
|
LogInfo("Instance changed succeed.\n");
|
|
instance = *impl;
|
|
}
|
|
else {
|
|
LogError("Can't changing the instance becase of using by some one.\n");
|
|
}
|
|
}
|
|
return instance;
|
|
}
|
|
void KeyManager::Init(void)
|
|
{
|
|
//
|
|
IHalCpp::GetInstance()->GetAllKeys(mAllKeyHal);
|
|
}
|
|
void KeyManager::UnInit(void)
|
|
{
|
|
//
|
|
mAllKeyHal.clear();
|
|
}
|
|
void KeyManager::StartTimer(void)
|
|
{
|
|
if (mAllKeyHal.size() == 0) {
|
|
LogError("StartTimer failed, no key to manager.\n");
|
|
return;
|
|
}
|
|
SetAllKeysMonitor();
|
|
auto timerThread = [](std::shared_ptr<KeyManager> timer) {
|
|
LogInfo("Key timer started.\n");
|
|
timer->Timer();
|
|
};
|
|
mTimer = std::thread(timerThread, shared_from_this());
|
|
}
|
|
void KeyManager::StopTimer(void)
|
|
{
|
|
mTimerRuning = false;
|
|
if (mTimer.joinable()) {
|
|
mTimer.join();
|
|
}
|
|
}
|
|
void KeyManager::Timer(void)
|
|
{
|
|
mTimerRuning = true;
|
|
std::map<std::string, std::shared_ptr<VKeyHal>>::iterator iter;
|
|
while (mTimerRuning) {
|
|
for (iter = mAllKeyHal.begin(); iter != mAllKeyHal.end(); ++iter) {
|
|
std::shared_ptr<VKeyHal> keyHal = iter->second;
|
|
keyHal->CheckKeyStatus();
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(PERIPHERAL_CHECK_PERIOD_MS));
|
|
}
|
|
}
|
|
void KeyManager::GetAllKeysState(std::map<std::string, KeyStatus> &status)
|
|
{
|
|
std::map<std::string, std::shared_ptr<VKeyHal>>::iterator iter;
|
|
for (iter = mAllKeyHal.begin(); iter != mAllKeyHal.end(); ++iter) {
|
|
std::shared_ptr<VKeyHal> keyHal = iter->second;
|
|
long int holdTimeMs = 0;
|
|
VirtualKeyEvent holdPressingEvent = 0x00;
|
|
keyHal->GetHoldPressingTimeMs(holdTimeMs, holdPressingEvent);
|
|
KeyStatus result(holdPressingEvent, holdTimeMs);
|
|
status.insert(std::make_pair(iter->first, result));
|
|
}
|
|
}
|
|
void KeyManager::SetAllKeysMonitor(void)
|
|
{
|
|
std::shared_ptr<VKeyHalMonitor> monitor = shared_from_this();
|
|
std::map<std::string, std::shared_ptr<VKeyHal>>::iterator iter;
|
|
for (iter = mAllKeyHal.begin(); iter != mAllKeyHal.end(); ++iter) {
|
|
std::shared_ptr<VKeyHal> keyHal = iter->second;
|
|
keyHal->SetKeyMonitor(monitor);
|
|
}
|
|
}
|
|
void KeyManager::KeyEventHappened(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs)
|
|
{
|
|
//
|
|
} |