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
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#define EVENT_DEF(event_name, ...) \
|
|
|
|
struct event_name final : public hex::Event<__VA_ARGS__> { \
|
|
|
|
constexpr static auto id = [] { return hex::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
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
class EventId {
|
|
|
|
public:
|
2021-08-21 13:53:50 +02:00
|
|
|
explicit constexpr EventId(const char *func = __builtin_FUNCTION(), u32 line = __builtin_LINE()) {
|
2022-08-06 21:38:09 +02:00
|
|
|
this->m_hash = line ^ 987654321;
|
2021-03-27 11:36:36 +01:00
|
|
|
for (auto c : std::string_view(func)) {
|
2022-01-24 20:53:17 +01:00
|
|
|
this->m_hash = (this->m_hash >> 5) | (this->m_hash << 27);
|
2021-03-27 11:36:36 +01:00
|
|
|
this->m_hash ^= c;
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
constexpr bool operator==(const EventId &rhs) const = default;
|
2021-01-11 21:11:03 +01:00
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
private:
|
|
|
|
u32 m_hash;
|
|
|
|
};
|
2021-01-11 21:11:03 +01:00
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
struct EventBase {
|
|
|
|
EventBase() noexcept = default;
|
2020-12-22 18:10:01 +01:00
|
|
|
};
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<typename... Params>
|
2021-03-27 11:36:36 +01:00
|
|
|
struct Event : public EventBase {
|
|
|
|
using Callback = std::function<void(Params...)>;
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
explicit Event(Callback func) noexcept : m_func(std::move(func)) { }
|
2021-03-27 11:36:36 +01:00
|
|
|
|
|
|
|
void operator()(Params... params) const noexcept {
|
|
|
|
this->m_func(params...);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Callback m_func;
|
2020-12-22 18:10:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class EventManager {
|
|
|
|
public:
|
2022-01-24 20:53:17 +01:00
|
|
|
using EventList = std::list<std::pair<EventId, EventBase *>>;
|
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)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename E>
|
|
|
|
static void subscribe(void *token, typename E::Callback function) {
|
|
|
|
s_tokenStore.insert(std::make_pair(token, subscribe<E>(function)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unsubscribe(EventList::iterator iter) noexcept {
|
|
|
|
s_events.remove(*iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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 *);
|
2021-03-27 11:36:36 +01:00
|
|
|
EVENT_DEF(EventRegionSelected, Region);
|
|
|
|
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);
|
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);
|
2021-03-29 22:44:23 +02:00
|
|
|
EVENT_DEF(RequestChangeWindowTitle, std::string);
|
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);
|
2021-09-16 22:23:51 +02:00
|
|
|
EVENT_DEF(RequestChangeTheme, u32);
|
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 **);
|
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()>);
|
|
|
|
EVENT_DEF(RequestShowFileChooserPopup, std::vector<std::fs::path>, std::vector<nfdfilteritem_t>, std::function<void(std::fs::path)>);
|
|
|
|
|
2022-05-27 20:42:07 +02:00
|
|
|
EVENT_DEF(QuerySelection, std::optional<Region> &);
|
2021-03-27 11:36:36 +01:00
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|