2020-10-30 22:36:11 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-10-30 22:36:11 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <vapours.hpp>
|
|
|
|
#include <stratosphere/os.hpp>
|
|
|
|
|
|
|
|
namespace ams::ddsf {
|
|
|
|
|
|
|
|
class EventHandlerManager;
|
|
|
|
|
|
|
|
class IEventHandler {
|
|
|
|
NON_COPYABLE(IEventHandler);
|
|
|
|
NON_MOVEABLE(IEventHandler);
|
|
|
|
friend class EventHandlerManager;
|
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
os::MultiWaitHolderType m_holder;
|
|
|
|
uintptr_t m_user_data;
|
|
|
|
bool m_is_initialized;
|
|
|
|
bool m_is_registered;
|
2020-10-30 22:36:11 +00:00
|
|
|
private:
|
2021-10-01 02:00:47 +00:00
|
|
|
void Link(os::MultiWaitType *multi_wait) {
|
2020-10-30 22:36:11 +00:00
|
|
|
AMS_ASSERT(this->IsInitialized());
|
|
|
|
AMS_ASSERT(!this->IsRegistered());
|
2021-10-01 02:00:47 +00:00
|
|
|
AMS_ASSERT(multi_wait != nullptr);
|
2021-10-10 07:14:06 +00:00
|
|
|
os::LinkMultiWaitHolder(multi_wait, std::addressof(m_holder));
|
2020-10-30 22:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Unlink() {
|
|
|
|
AMS_ASSERT(this->IsInitialized());
|
|
|
|
AMS_ASSERT(this->IsRegistered());
|
2021-10-10 07:14:06 +00:00
|
|
|
os::UnlinkMultiWaitHolder(std::addressof(m_holder));
|
2020-10-30 22:36:11 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
static IEventHandler &ToEventHandler(os::MultiWaitHolderType *holder) {
|
2020-10-30 22:36:11 +00:00
|
|
|
AMS_ASSERT(holder != nullptr);
|
2021-10-01 02:00:47 +00:00
|
|
|
auto &event_handler = *reinterpret_cast<IEventHandler *>(os::GetMultiWaitHolderUserData(holder));
|
2020-10-30 22:36:11 +00:00
|
|
|
AMS_ASSERT(event_handler.IsInitialized());
|
|
|
|
return event_handler;
|
|
|
|
}
|
|
|
|
public:
|
2021-10-10 07:14:06 +00:00
|
|
|
IEventHandler() : m_holder(), m_user_data(0), m_is_initialized(false), m_is_registered(false) { /* ... */ }
|
2020-10-30 22:36:11 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
virtual ~IEventHandler() {
|
2020-10-30 22:36:11 +00:00
|
|
|
if (this->IsRegistered()) {
|
|
|
|
this->Unlink();
|
|
|
|
}
|
|
|
|
if (this->IsInitialized()) {
|
|
|
|
this->Finalize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
bool IsInitialized() const { return m_is_initialized; }
|
|
|
|
bool IsRegistered() const { return m_is_registered; }
|
2020-10-30 22:36:11 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
uintptr_t GetUserData() const { return m_user_data; }
|
|
|
|
void SetUserData(uintptr_t d) { m_user_data = d; }
|
2020-10-30 22:36:11 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void Initialize(T *object) {
|
|
|
|
AMS_ASSERT(object != nullptr);
|
|
|
|
AMS_ASSERT(!this->IsInitialized());
|
2021-10-10 07:14:06 +00:00
|
|
|
os::InitializeMultiWaitHolder(std::addressof(m_holder), object);
|
|
|
|
os::SetMultiWaitHolderUserData(std::addressof(m_holder), reinterpret_cast<uintptr_t>(this));
|
|
|
|
m_is_initialized = true;
|
|
|
|
m_is_registered = false;
|
2020-10-30 22:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Finalize() {
|
|
|
|
AMS_ASSERT(this->IsInitialized());
|
|
|
|
AMS_ASSERT(!this->IsRegistered());
|
2021-10-10 07:14:06 +00:00
|
|
|
os::FinalizeMultiWaitHolder(std::addressof(m_holder));
|
|
|
|
m_is_initialized = false;
|
|
|
|
m_is_registered = false;
|
2020-10-30 22:36:11 +00:00
|
|
|
}
|
|
|
|
protected:
|
|
|
|
virtual void HandleEvent() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|