#include "ehs/io/hid/InputHandler.h" #include "ehs/system/CPU.h" namespace ehs { InputHandler::~InputHandler() { for (UInt_64 i = 0; i < devices.Size(); i++) delete devices[i]; } InputHandler::InputHandler() : hashId(0), start(0) { } InputHandler::InputHandler(Str_8 id) : hashId(id.Hash_64()), id((Str_8&&)id), start(CPU::GetTSC()) { } InputHandler::InputHandler(InputHandler&& ih) noexcept : hashId(ih.hashId), id((Str_8&&)ih.id), devices((Array&&)ih.devices), start(ih.start) { ih.hashId = 0; ih.start = 0; } InputHandler::InputHandler(const InputHandler& ih) : hashId(ih.hashId), id(ih.id), devices(ih.devices.Size()), start(ih.start) { for (UInt_64 i = 0; i < devices.Size(); i++) devices[i] = ih.devices[i]->Clone(); } InputHandler& InputHandler::operator=(InputHandler&& ih) noexcept { if (this == &ih) return *this; hashId = ih.hashId; id = (Str_8&&)ih.id; devices = (Array&&)ih.devices; start = ih.start; ih.hashId = 0; ih.start = 0; return *this; } InputHandler& InputHandler::operator=(const InputHandler& ih) { if (this == &ih) return *this; for (UInt_64 i = 0; i < devices.Size(); i++) delete devices[i]; hashId = ih.hashId; id = ih.id; devices = Array(ih.devices.Size()); start = ih.start; for (UInt_64 i = 0; i < devices.Size(); i++) devices[i] = ih.devices[i]->Clone(); return *this; } bool InputHandler::operator==(const UInt_64 otherHashId) const { return hashId == otherHashId; } bool InputHandler::operator!=(const UInt_64 otherHashId) const { return hashId != otherHashId; } bool InputHandler::Initialize() { if (IsInitialized()) return false; return true; } bool InputHandler::Release() { if (!IsInitialized()) return false; for (UInt_64 i = 0; i < devices.Size(); i++) delete devices[i]; devices.Clear(); return true; } void InputHandler::Poll() { static UInt_64 freq = CPU::GetTSC_Freq(); const UInt_64 newTSC = CPU::GetTSC(); delta = newTSC - start; start = newTSC; for (UInt_64 i = 0; i < devices.Size(); i++) devices[i]->Poll((float)delta / (float)freq); } UInt_64 InputHandler::GetHashId() const { return hashId; } Str_8 InputHandler::GetId() const { return id; } void InputHandler::ResetAllStates() { for (UInt_64 i = 0; i < devices.Size(); i++) devices[i]->ReleaseAll(); } bool InputHandler::HasDevice(const UInt_64 id) const { for (UInt_64 i = 0; i < devices.Size(); i++) if (*devices[i] == id) return true; return false; } bool InputHandler::AddDevice(HID* device) { if (HasDevice(device->GetId())) return false; devices.Push(device); return true; } HID* InputHandler::GetDevice(const UInt_64 id) const { for (UInt_64 i = 0; i < devices.Size(); i++) if (*devices[i] == id) return devices[i]; return nullptr; } HID* InputHandler::GetDeviceByType(const UInt_8 type) const { for (UInt_64 i = 0; i < devices.Size(); i++) if (devices[i]->GetType() == type) return devices[i]; return nullptr; } bool InputHandler::IsInitialized() const { return false; } }