hunting/utils/KeyControl/include/KeyControl.h
2024-02-16 10:48:14 -08:00

75 lines
2.5 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.
*/
#ifndef KEY_CONTROL_H
#define KEY_CONTROL_H
#include <functional>
#include <memory>
#include <mutex>
constexpr long int KEY_DO_NOT_HOLD_PRESSING = -1;
constexpr int KEY_ACTION_LONG_CLICK = 1000 * 5;
constexpr int KEY_ACTION_SHORT_CLICK = 200;
constexpr int KEY_ACTION_HOLD_DWON = 500;
constexpr long int KEY_NOT_PRESSING = -1;
enum class KeyHalEvent
{
PRESSING = 0,
NOT_PRESSING,
END
};
enum class KeyEvent
{
SHORT_CLICK = 0,
HOLD_DOWN,
HOLD_UP,
END
};
class VKeyControl
{
public:
VKeyControl() = default;
virtual ~VKeyControl() = default;
virtual const std::string GetKeyName(void) { return "undefine"; }
virtual unsigned int GetStatusCheckPeriodMs(void) { return 0; }
virtual void KeyHalEventTrigger(const KeyHalEvent &event) {}
virtual void KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs) {}
virtual void TimerKeyEventTrigger(const KeyHalEvent &event) {}
virtual long int GetHoldPressingTimeMs(void) { return KEY_DO_NOT_HOLD_PRESSING; }
};
// using KeyActionReport = std::function<void(const std::string &, const KeyEvent &, const unsigned int &)>;
class KeyControl : public VKeyControl, public std::enable_shared_from_this<KeyControl>
{
public:
KeyControl();
// KeyControl(std::shared_ptr<VKeyControl> &keyHal, const KeyActionReport &keyAction,
// const long int &longClickTime = KEY_ACTION_LONG_CLICK);
~KeyControl();
void KeyHalEventTrigger(const KeyHalEvent &event) override;
void TimerKeyEventTrigger(const KeyHalEvent &event) override;
long int GetHoldPressingTimeMs(void) override;
void Init(void);
void UnInit(void);
void ActionReport(const std::string &key, const KeyHalEvent &keyEvent);
private:
void KeyPressingTrigger(const std::string &key);
void KeyNotPressingTrigger(const std::string &key);
bool IsKeyPressing(void);
private:
std::mutex mMutex;
long int mPressingTime;
long int mLongClickTime;
};
#endif