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-01-16 01:51:31 +01:00
|
|
|
#include <hex/helpers/paths.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
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
class 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()) {
|
2021-03-27 11:36:36 +01:00
|
|
|
this->m_hash = line ^ 123456789;
|
|
|
|
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)
|
2021-08-21 13:53:50 +02:00
|
|
|
(*reinterpret_cast<E *>(event))(std::forward<decltype(args)>(args)...);
|
2021-03-27 11:36:36 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-24 20:53:17 +01:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
namespace pl {
|
|
|
|
class PatternData;
|
|
|
|
}
|
2022-01-12 22:02:47 +01:00
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
/* Default Events */
|
2022-01-16 01:51:31 +01:00
|
|
|
EVENT_DEF(EventFileLoaded, fs::path);
|
2021-03-29 22:44:35 +02:00
|
|
|
EVENT_DEF(EventFileUnloaded);
|
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(EventProjectFileStore);
|
|
|
|
EVENT_DEF(EventProjectFileLoad);
|
|
|
|
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-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-01-16 01:51:31 +01:00
|
|
|
EVENT_DEF(RequestOpenFile, 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);
|
2021-12-07 22:47:41 +01:00
|
|
|
EVENT_DEF(RequestCreateProvider, std::string, hex::prv::Provider **);
|
2021-08-21 13:53:50 +02:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
EVENT_DEF(QuerySelection, Region &);
|
2021-03-27 11:36:36 +01:00
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|