1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-30 07:42:13 +00:00
Atmosphere/libraries/libstratosphere/include/stratosphere/pgl/pgl_event_observer.hpp

125 lines
4.6 KiB
C++
Raw Normal View History

2020-04-16 01:37:11 +01:00
/*
* Copyright (c) Atmosphère-NX
2020-04-16 01:37:11 +01: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 <stratosphere/os.hpp>
#include <stratosphere/pm.hpp>
#include <stratosphere/pgl/pgl_types.hpp>
#include <stratosphere/pgl/sf/pgl_sf_i_event_observer.hpp>
2021-04-11 09:07:55 +01:00
#include <stratosphere/pgl/tipc/pgl_tipc_i_event_observer.hpp>
2020-04-16 01:37:11 +01:00
namespace ams::pgl {
2021-04-11 09:07:55 +01:00
namespace impl {
class EventObserverInterface {
NON_COPYABLE(EventObserverInterface);
NON_MOVEABLE(EventObserverInterface);
public:
constexpr EventObserverInterface() = default;
virtual ~EventObserverInterface() { /* ... */ }
virtual Result GetSystemEvent(os::SystemEventType *out) = 0;
virtual Result GetProcessEventInfo(pm::ProcessEventInfo *out) = 0;
};
class EventObserverByCmif final : public EventObserverInterface {
NON_COPYABLE(EventObserverByCmif);
NON_MOVEABLE(EventObserverByCmif);
private:
ams::sf::SharedPointer<pgl::sf::IEventObserver> m_cmif_interface;
public:
explicit EventObserverByCmif(ams::sf::SharedPointer<pgl::sf::IEventObserver> intf) : m_cmif_interface(intf) { /* ... */ }
public:
virtual Result GetSystemEvent(os::SystemEventType *out) override {
ams::sf::NativeHandle handle;
2021-04-11 09:07:55 +01:00
R_TRY(m_cmif_interface->GetProcessEventHandle(std::addressof(handle)));
os::AttachReadableHandleToSystemEvent(out, handle.GetOsHandle(), handle.IsManaged(), os::EventClearMode_AutoClear);
handle.Detach();
2022-03-26 07:14:36 +00:00
R_SUCCEED();
2021-04-11 09:07:55 +01:00
}
virtual Result GetProcessEventInfo(pm::ProcessEventInfo *out) override {
R_RETURN(m_cmif_interface->GetProcessEventInfo(out));
2021-04-11 09:07:55 +01:00
}
};
template<typename T> requires tipc::IsIEventObserver<T>
class EventObserverByTipc final : public EventObserverInterface {
NON_COPYABLE(EventObserverByTipc);
NON_MOVEABLE(EventObserverByTipc);
private:
T m_tipc_interface;
public:
template<typename... Args>
explicit EventObserverByTipc(Args &&... args) : m_tipc_interface(std::forward<Args>(args)...) { /* ... */ }
public:
virtual Result GetSystemEvent(os::SystemEventType *out) override {
2021-10-12 08:20:21 +01:00
os::NativeHandle handle;
2021-04-11 09:07:55 +01:00
R_TRY(m_tipc_interface.GetProcessEventHandle(std::addressof(handle)));
2021-10-12 08:20:21 +01:00
os::AttachReadableHandleToSystemEvent(out, handle, true, os::EventClearMode_AutoClear);
2022-03-26 07:14:36 +00:00
R_SUCCEED();
2021-04-11 09:07:55 +01:00
}
virtual Result GetProcessEventInfo(pm::ProcessEventInfo *out) override {
R_RETURN(m_tipc_interface.GetProcessEventInfo(ams::tipc::Out<pm::ProcessEventInfo>(out)));
2021-04-11 09:07:55 +01:00
}
};
}
2020-04-16 01:37:11 +01:00
class EventObserver {
NON_COPYABLE(EventObserver);
private:
2021-07-28 07:55:53 +01:00
struct Deleter {
void operator()(impl::EventObserverInterface *);
};
public:
using UniquePtr = std::unique_ptr<impl::EventObserverInterface, Deleter>;
private:
UniquePtr m_impl;
2020-04-16 01:37:11 +01:00
public:
EventObserver() { /* ... */ }
2021-04-11 09:07:55 +01:00
2021-07-28 07:55:53 +01:00
explicit EventObserver(UniquePtr impl) : m_impl(std::move(impl)) { /* ... */ }
2020-04-16 01:37:11 +01:00
EventObserver(EventObserver &&rhs) {
2021-04-11 09:07:55 +01:00
m_impl = std::move(rhs.m_impl);
2020-04-16 01:37:11 +01:00
}
EventObserver &operator=(EventObserver &&rhs) {
EventObserver(std::move(rhs)).Swap(*this);
return *this;
}
void Swap(EventObserver &rhs) {
2021-04-11 09:07:55 +01:00
std::swap(m_impl, rhs.m_impl);
2020-04-16 01:37:11 +01:00
}
public:
Result GetSystemEvent(os::SystemEventType *out) {
R_RETURN(m_impl->GetSystemEvent(out));
2020-04-16 01:37:11 +01:00
}
Result GetProcessEventInfo(pm::ProcessEventInfo *out) {
R_RETURN(m_impl->GetProcessEventInfo(out));
2020-04-16 01:37:11 +01:00
}
};
}