2021-01-20 20:16:24 +01:00
|
|
|
#include <hex/api/imhex_api.hpp>
|
|
|
|
|
|
|
|
#include <hex/api/event.hpp>
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
2022-02-16 14:57:13 +01:00
|
|
|
#include <utility>
|
2021-01-20 20:16:24 +01:00
|
|
|
|
2021-08-21 15:03:44 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-02-17 11:42:56 +01:00
|
|
|
#include <imgui.h>
|
|
|
|
|
2021-01-20 20:16:24 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
namespace ImHexApi::Common {
|
|
|
|
|
|
|
|
void closeImHex(bool noQuestions) {
|
|
|
|
EventManager::post<RequestCloseImHex>(noQuestions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void restartImHex() {
|
|
|
|
EventManager::post<RequestCloseImHex>(false);
|
|
|
|
std::atexit([] {
|
|
|
|
auto &programArgs = ImHexApi::System::getProgramArguments();
|
|
|
|
execve(programArgs.argv[0], programArgs.argv, programArgs.envp);
|
|
|
|
});
|
|
|
|
}
|
2021-08-21 13:55:21 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
namespace ImHexApi::HexEditor {
|
2021-01-20 20:16:24 +01:00
|
|
|
|
2022-02-16 14:57:13 +01:00
|
|
|
Highlighting::Highlighting(Region region, color_t color, std::string tooltip)
|
|
|
|
: m_region(region), m_color(color), m_tooltip(std::move(tooltip)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
static std::map<u32, Highlighting> s_highlights;
|
|
|
|
std::map<u32, Highlighting> &getHighlights() {
|
|
|
|
return s_highlights;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::map<u32, HighlightingFunction> s_highlightingFunctions;
|
|
|
|
std::map<u32, HighlightingFunction> &getHighlightingFunctions() {
|
|
|
|
return s_highlightingFunctions;
|
|
|
|
}
|
2021-01-20 20:16:24 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
}
|
2021-01-20 20:16:24 +01:00
|
|
|
|
2022-02-16 14:57:13 +01:00
|
|
|
u32 addHighlight(const Region ®ion, color_t color, const std::string &tooltip) {
|
|
|
|
auto &highlights = impl::getHighlights();
|
2022-03-26 16:54:15 +01:00
|
|
|
static u64 id = 0;
|
|
|
|
|
|
|
|
id++;
|
2021-01-20 20:16:24 +01:00
|
|
|
|
2022-02-16 14:57:13 +01:00
|
|
|
highlights.insert({
|
2022-02-01 22:09:44 +01:00
|
|
|
id, Highlighting {region, color, tooltip}
|
|
|
|
});
|
2021-01-20 20:16:24 +01:00
|
|
|
|
2022-02-04 00:29:47 +01:00
|
|
|
EventManager::post<EventHighlightingChanged>();
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeHighlight(u32 id) {
|
2022-02-16 14:57:13 +01:00
|
|
|
impl::getHighlights().erase(id);
|
|
|
|
|
|
|
|
EventManager::post<EventHighlightingChanged>();
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 addHighlightingProvider(const impl::HighlightingFunction &function) {
|
|
|
|
auto &highlightFuncs = impl::getHighlightingFunctions();
|
|
|
|
|
|
|
|
auto id = highlightFuncs.size();
|
|
|
|
|
|
|
|
highlightFuncs.insert({ id, function });
|
2022-02-04 00:29:47 +01:00
|
|
|
|
|
|
|
EventManager::post<EventHighlightingChanged>();
|
2022-02-16 14:57:13 +01:00
|
|
|
|
|
|
|
return id;
|
2022-02-01 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
2022-02-16 14:57:13 +01:00
|
|
|
void removeHighlightingProvider(u32 id) {
|
|
|
|
impl::getHighlightingFunctions().erase(id);
|
|
|
|
|
|
|
|
EventManager::post<EventHighlightingChanged>();
|
2022-02-01 18:09:40 +01:00
|
|
|
}
|
2021-01-20 20:16:24 +01:00
|
|
|
|
2022-02-08 18:38:54 +01:00
|
|
|
Region getSelection() {
|
|
|
|
static Region selectedRegion;
|
|
|
|
EventManager::subscribe<EventRegionSelected>([](const Region ®ion) {
|
|
|
|
selectedRegion = region;
|
|
|
|
});
|
|
|
|
|
|
|
|
return selectedRegion;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSelection(const Region ®ion) {
|
|
|
|
EventManager::post<RequestSelectionChange>(region);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSelection(u64 address, size_t size) {
|
|
|
|
setSelection({ address, size });
|
|
|
|
}
|
|
|
|
|
2021-01-20 20:16:24 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
namespace ImHexApi::Bookmarks {
|
2021-09-21 02:29:54 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
void add(Region region, const std::string &name, const std::string &comment, u32 color) {
|
|
|
|
EventManager::post<RequestAddBookmark>(region, name, comment, color);
|
|
|
|
}
|
2021-09-21 02:29:54 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
void add(u64 address, size_t size, const std::string &name, const std::string &comment, u32 color) {
|
|
|
|
add(Region { address, size }, name, comment, color);
|
|
|
|
}
|
2021-09-21 02:29:54 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-12 00:41:44 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
namespace ImHexApi::Provider {
|
|
|
|
|
|
|
|
static u32 s_currentProvider;
|
2022-02-01 22:09:44 +01:00
|
|
|
static std::vector<prv::Provider *> s_providers;
|
2022-02-01 18:09:40 +01:00
|
|
|
|
|
|
|
prv::Provider *get() {
|
|
|
|
if (!ImHexApi::Provider::isValid())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return s_providers[s_currentProvider];
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<prv::Provider *> &getProviders() {
|
|
|
|
return s_providers;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setCurrentProvider(u32 index) {
|
|
|
|
if (index < s_providers.size()) {
|
2022-02-01 22:09:44 +01:00
|
|
|
auto oldProvider = get();
|
2022-02-01 18:09:40 +01:00
|
|
|
s_currentProvider = index;
|
|
|
|
EventManager::post<EventProviderChanged>(oldProvider, get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isValid() {
|
|
|
|
return !s_providers.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(prv::Provider *provider) {
|
|
|
|
s_providers.push_back(provider);
|
|
|
|
setCurrentProvider(s_providers.size() - 1);
|
|
|
|
|
|
|
|
EventManager::post<EventProviderCreated>(provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove(prv::Provider *provider) {
|
|
|
|
auto it = std::find(s_providers.begin(), s_providers.end(), provider);
|
|
|
|
|
|
|
|
s_providers.erase(it);
|
|
|
|
|
|
|
|
if (it - s_providers.begin() == s_currentProvider)
|
2022-02-05 22:26:00 +01:00
|
|
|
setCurrentProvider(0);
|
2022-02-01 18:09:40 +01:00
|
|
|
|
|
|
|
delete provider;
|
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
namespace ImHexApi::Tasks {
|
|
|
|
|
|
|
|
Task createTask(const std::string &unlocalizedName, u64 maxValue) {
|
2022-03-04 20:52:39 +01:00
|
|
|
return { unlocalizedName, maxValue };
|
2022-02-01 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void doLater(const std::function<void()> &function) {
|
2022-03-15 23:46:02 +01:00
|
|
|
static std::mutex tasksMutex;
|
|
|
|
std::scoped_lock lock(tasksMutex);
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
getDeferredCalls().push_back(function);
|
|
|
|
}
|
|
|
|
|
2022-02-01 22:09:44 +01:00
|
|
|
std::vector<std::function<void()>> &getDeferredCalls() {
|
2022-03-15 23:46:02 +01:00
|
|
|
static std::vector<std::function<void()>> deferredCalls;
|
|
|
|
|
|
|
|
return deferredCalls;
|
2022-02-01 18:09:40 +01:00
|
|
|
}
|
2021-09-21 02:29:54 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-16 23:48:52 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
namespace ImHexApi::System {
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
static ImVec2 s_mainWindowPos;
|
|
|
|
static ImVec2 s_mainWindowSize;
|
|
|
|
void setMainWindowPosition(u32 x, u32 y) {
|
|
|
|
s_mainWindowPos = ImVec2(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setMainWindowSize(u32 width, u32 height) {
|
|
|
|
s_mainWindowSize = ImVec2(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ImGuiID s_mainDockSpaceId;
|
|
|
|
void setMainDockSpaceId(ImGuiID id) {
|
|
|
|
s_mainDockSpaceId = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static float s_globalScale;
|
|
|
|
void setGlobalScale(float scale) {
|
|
|
|
s_globalScale = scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ProgramArguments s_programArguments;
|
|
|
|
void setProgramArguments(int argc, char **argv, char **envp) {
|
|
|
|
s_programArguments.argc = argc;
|
|
|
|
s_programArguments.argv = argv;
|
|
|
|
s_programArguments.envp = envp;
|
|
|
|
}
|
|
|
|
|
2022-02-15 23:07:48 +01:00
|
|
|
static bool s_borderlessWindowMode;
|
|
|
|
void setBorderlessWindowMode(bool enabled) {
|
|
|
|
s_borderlessWindowMode = enabled;
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-01 22:09:44 +01:00
|
|
|
const ProgramArguments &getProgramArguments() {
|
2022-02-01 18:09:40 +01:00
|
|
|
return impl::s_programArguments;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static float s_targetFPS = 60.0F;
|
|
|
|
|
|
|
|
float getTargetFPS() {
|
|
|
|
return s_targetFPS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setTargetFPS(float fps) {
|
|
|
|
s_targetFPS = fps;
|
|
|
|
}
|
|
|
|
|
|
|
|
float getGlobalScale() {
|
|
|
|
return impl::s_globalScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImVec2 getMainWindowPosition() {
|
|
|
|
return impl::s_mainWindowPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImVec2 getMainWindowSize() {
|
|
|
|
return impl::s_mainWindowSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImGuiID getMainDockSpaceId() {
|
|
|
|
return impl::s_mainDockSpaceId;
|
|
|
|
}
|
|
|
|
|
2022-02-15 23:07:48 +01:00
|
|
|
bool isBorderlessWindowModeEnabled() {
|
|
|
|
return impl::s_borderlessWindowMode;
|
|
|
|
}
|
|
|
|
|
2022-02-01 22:09:44 +01:00
|
|
|
std::map<std::string, std::string> &getInitArguments() {
|
2022-02-01 18:09:40 +01:00
|
|
|
static std::map<std::string, std::string> initArgs;
|
|
|
|
|
|
|
|
return initArgs;
|
|
|
|
}
|
2021-12-16 23:48:52 +01:00
|
|
|
}
|
|
|
|
|
2021-12-22 13:16:51 +01:00
|
|
|
}
|