2020-12-22 18:10:01 +01:00
|
|
|
#pragma once
|
|
|
|
|
2021-01-11 21:11:03 +01:00
|
|
|
#include <hex.hpp>
|
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <string_view>
|
2020-12-22 18:10:01 +01:00
|
|
|
#include <functional>
|
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
#include <hex/api/imhex_api.hpp>
|
2022-03-04 11:36:37 +01:00
|
|
|
#include <hex/helpers/fs.hpp>
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
#define EVENT_DEF(event_name, ...) \
|
|
|
|
struct event_name final : public hex::impl::Event<__VA_ARGS__> { \
|
|
|
|
constexpr static auto id = [] { return hex::impl::EventId(); }(); \
|
|
|
|
explicit event_name(Callback func) noexcept : Event(std::move(func)) { } \
|
2021-09-08 15:18:24 +02:00
|
|
|
}
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
struct GLFWwindow;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
namespace hex {
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
class EventId {
|
|
|
|
public:
|
|
|
|
explicit constexpr EventId(const char *func = __builtin_FUNCTION(), u32 line = __builtin_LINE()) {
|
|
|
|
this->m_hash = line ^ 987654321;
|
|
|
|
for (auto c : std::string_view(func)) {
|
|
|
|
this->m_hash = (this->m_hash >> 5) | (this->m_hash << 27);
|
|
|
|
this->m_hash ^= c;
|
|
|
|
}
|
2021-03-27 11:36:36 +01:00
|
|
|
}
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
constexpr bool operator==(const EventId &rhs) const = default;
|
2021-01-11 21:11:03 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
private:
|
|
|
|
u32 m_hash;
|
|
|
|
};
|
2021-01-11 21:11:03 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
struct EventBase {
|
|
|
|
EventBase() noexcept = default;
|
|
|
|
};
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
template<typename... Params>
|
|
|
|
struct Event : public EventBase {
|
|
|
|
using Callback = std::function<void(Params...)>;
|
2021-03-27 11:36:36 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
explicit Event(Callback func) noexcept : m_func(std::move(func)) { }
|
2021-03-27 11:36:36 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
void operator()(Params... params) const noexcept {
|
|
|
|
this->m_func(params...);
|
|
|
|
}
|
2021-03-27 11:36:36 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
private:
|
|
|
|
Callback m_func;
|
|
|
|
};
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The EventManager allows subscribing to and posting events to different parts of the program.
|
|
|
|
* To create a new event, use the EVENT_DEF macro. This will create a new event type with the given name and parameters
|
|
|
|
*/
|
2020-12-22 18:10:01 +01:00
|
|
|
class EventManager {
|
|
|
|
public:
|
2023-03-21 15:33:43 +01:00
|
|
|
using EventList = std::list<std::pair<impl::EventId, impl::EventBase *>>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Subscribes to an event
|
|
|
|
* @tparam E Event
|
|
|
|
* @param function Function to call when the event is posted
|
|
|
|
* @return Token to unsubscribe from the event
|
|
|
|
*/
|
2021-03-27 11:36:36 +01:00
|
|
|
template<typename E>
|
2022-02-08 18:38:54 +01:00
|
|
|
static EventList::iterator subscribe(typename E::Callback function) {
|
2021-03-27 11:36:36 +01:00
|
|
|
return s_events.insert(s_events.end(), std::make_pair(E::id, new E(function)));
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Subscribes to an event
|
|
|
|
* @tparam E Event
|
|
|
|
* @param token Unique token to register the event to. Later required to unsubscribe again
|
|
|
|
* @param function Function to call when the event is posted
|
|
|
|
*/
|
2021-03-27 11:36:36 +01:00
|
|
|
template<typename E>
|
|
|
|
static void subscribe(void *token, typename E::Callback function) {
|
|
|
|
s_tokenStore.insert(std::make_pair(token, subscribe<E>(function)));
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Unsubscribes from an event
|
|
|
|
* @param token Token returned by subscribe
|
|
|
|
*/
|
|
|
|
static void unsubscribe(const EventList::iterator &token) noexcept {
|
|
|
|
s_events.remove(*token);
|
2021-03-27 11:36:36 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Unsubscribes from an event
|
|
|
|
* @tparam E Event
|
|
|
|
* @param token Token passed to subscribe
|
|
|
|
*/
|
2021-03-27 11:36:36 +01:00
|
|
|
template<typename E>
|
|
|
|
static void unsubscribe(void *token) noexcept {
|
2022-01-24 20:53:17 +01:00
|
|
|
auto iter = std::find_if(s_tokenStore.begin(), s_tokenStore.end(), [&](auto &item) {
|
2021-03-27 11:36:36 +01:00
|
|
|
return item.first == token && item.second->first == E::id;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (iter != s_tokenStore.end()) {
|
|
|
|
s_events.remove(*iter->second);
|
2023-01-01 01:01:24 +01:00
|
|
|
s_tokenStore.erase(iter);
|
2021-03-27 11:36:36 +01:00
|
|
|
}
|
2023-01-01 01:01:24 +01:00
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Posts an event to all subscribers of it
|
|
|
|
* @tparam E Event
|
|
|
|
* @param args Arguments to pass to the event
|
|
|
|
*/
|
2021-03-27 11:36:36 +01:00
|
|
|
template<typename E>
|
2022-01-24 20:53:17 +01:00
|
|
|
static void post(auto &&...args) noexcept {
|
2021-03-27 11:36:36 +01:00
|
|
|
for (const auto &[id, event] : s_events) {
|
|
|
|
if (id == E::id)
|
2022-04-15 19:02:36 +02:00
|
|
|
(*static_cast<E *const>(event))(std::forward<decltype(args)>(args)...);
|
2021-03-27 11:36:36 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Unsubscribe all subscribers from all events
|
|
|
|
*/
|
2022-08-03 10:45:50 +02:00
|
|
|
static void clear() noexcept {
|
|
|
|
s_events.clear();
|
|
|
|
s_tokenStore.clear();
|
|
|
|
}
|
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
private:
|
2022-01-24 20:53:17 +01:00
|
|
|
static std::map<void *, EventList::iterator> s_tokenStore;
|
2021-03-27 11:36:36 +01:00
|
|
|
static EventList s_events;
|
2020-12-22 18:10:01 +01:00
|
|
|
};
|
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
/* Default Events */
|
2022-03-04 11:36:37 +01:00
|
|
|
EVENT_DEF(EventFileLoaded, std::fs::path);
|
2021-03-27 11:36:36 +01:00
|
|
|
EVENT_DEF(EventDataChanged);
|
2022-02-04 00:29:47 +01:00
|
|
|
EVENT_DEF(EventHighlightingChanged);
|
2022-01-24 20:53:17 +01:00
|
|
|
EVENT_DEF(EventWindowClosing, GLFWwindow *);
|
2022-11-08 21:43:22 +01:00
|
|
|
EVENT_DEF(EventRegionSelected, ImHexApi::HexEditor::ProviderRegion);
|
2021-03-27 11:36:36 +01:00
|
|
|
EVENT_DEF(EventSettingsChanged);
|
2021-08-17 13:41:44 +02:00
|
|
|
EVENT_DEF(EventAbnormalTermination, int);
|
2021-09-16 22:23:51 +02:00
|
|
|
EVENT_DEF(EventOSThemeChanged);
|
2022-01-24 20:53:17 +01:00
|
|
|
EVENT_DEF(EventProviderCreated, prv::Provider *);
|
2022-02-01 18:09:40 +01:00
|
|
|
EVENT_DEF(EventProviderChanged, prv::Provider *, prv::Provider *);
|
2022-08-08 21:23:52 +02:00
|
|
|
EVENT_DEF(EventProviderOpened, prv::Provider *);
|
|
|
|
EVENT_DEF(EventProviderClosing, prv::Provider *, bool *);
|
|
|
|
EVENT_DEF(EventProviderClosed, prv::Provider *);
|
2022-07-30 22:01:49 +02:00
|
|
|
EVENT_DEF(EventProviderDeleted, prv::Provider *);
|
2022-01-18 00:10:10 +01:00
|
|
|
EVENT_DEF(EventFrameBegin);
|
|
|
|
EVENT_DEF(EventFrameEnd);
|
2022-02-15 22:50:04 +01:00
|
|
|
EVENT_DEF(EventWindowInitialized);
|
2023-01-14 14:21:16 +01:00
|
|
|
EVENT_DEF(EventSetTaskBarIconState, u32, u32, u32);
|
2021-03-27 11:36:36 +01:00
|
|
|
|
|
|
|
EVENT_DEF(RequestOpenWindow, std::string);
|
|
|
|
EVENT_DEF(RequestSelectionChange, Region);
|
2022-02-01 18:09:40 +01:00
|
|
|
EVENT_DEF(RequestAddBookmark, Region, std::string, std::string, color_t);
|
2021-09-26 21:18:25 +02:00
|
|
|
EVENT_DEF(RequestSetPatternLanguageCode, std::string);
|
2023-01-07 17:31:22 +01:00
|
|
|
EVENT_DEF(RequestUpdateWindowTitle);
|
2021-08-21 13:53:50 +02:00
|
|
|
EVENT_DEF(RequestCloseImHex, bool);
|
2022-08-16 11:48:37 +02:00
|
|
|
EVENT_DEF(RequestRestartImHex);
|
2022-03-04 11:36:37 +01:00
|
|
|
EVENT_DEF(RequestOpenFile, std::fs::path);
|
2022-12-29 19:26:00 +01:00
|
|
|
EVENT_DEF(RequestChangeTheme, std::string);
|
2021-09-26 21:18:25 +02:00
|
|
|
EVENT_DEF(RequestOpenPopup, std::string);
|
2022-08-14 10:07:45 +02:00
|
|
|
EVENT_DEF(RequestCreateProvider, std::string, bool, hex::prv::Provider **);
|
2023-02-16 18:06:40 +01:00
|
|
|
EVENT_DEF(RequestInitThemeHandlers);
|
2021-08-21 13:53:50 +02:00
|
|
|
|
2022-07-02 17:53:13 +02:00
|
|
|
EVENT_DEF(RequestShowInfoPopup, std::string);
|
|
|
|
EVENT_DEF(RequestShowErrorPopup, std::string);
|
|
|
|
EVENT_DEF(RequestShowFatalErrorPopup, std::string);
|
|
|
|
EVENT_DEF(RequestShowYesNoQuestionPopup, std::string, std::function<void()>, std::function<void()>);
|
2023-01-28 00:01:53 +01:00
|
|
|
EVENT_DEF(RequestShowFileChooserPopup, std::vector<std::fs::path>, std::vector<nfdfilteritem_t>, std::function<void(std::fs::path)>, bool);
|
2022-07-02 17:53:13 +02:00
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|