2021-04-20 21:46:48 +02:00
|
|
|
#include "init/splash_window.hpp"
|
2021-04-17 15:46:26 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/imhex_api.hpp>
|
2021-04-17 15:46:26 +02:00
|
|
|
#include <hex/helpers/utils.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
|
|
|
#include <hex/helpers/logger.hpp>
|
2021-12-22 15:06:16 +01:00
|
|
|
|
|
|
|
#include <romfs/romfs.hpp>
|
2021-04-17 15:46:26 +02:00
|
|
|
|
|
|
|
#include <imgui.h>
|
2021-08-04 18:57:53 +02:00
|
|
|
#define IMGUI_DEFINE_MATH_OPERATORS
|
2021-04-17 15:46:26 +02:00
|
|
|
#include <imgui_internal.h>
|
2021-12-31 01:10:06 +01:00
|
|
|
#include <hex/ui/imgui_imhex_extensions.h>
|
2021-04-17 15:46:26 +02:00
|
|
|
#include <imgui_impl_glfw.h>
|
|
|
|
#include <imgui_impl_opengl3.h>
|
2022-07-26 14:59:08 +02:00
|
|
|
#include <fonts/fontawesome_font.h>
|
2021-04-17 15:46:26 +02:00
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <chrono>
|
2021-08-29 14:18:45 +02:00
|
|
|
#include <future>
|
|
|
|
#include <numeric>
|
2021-04-17 15:46:26 +02:00
|
|
|
|
|
|
|
using namespace std::literals::chrono_literals;
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
namespace hex::init {
|
2021-04-17 15:46:26 +02:00
|
|
|
|
2021-12-30 23:21:32 +01:00
|
|
|
WindowSplash::WindowSplash() : m_window(nullptr) {
|
2021-04-17 15:46:26 +02:00
|
|
|
this->initGLFW();
|
|
|
|
this->initImGui();
|
2022-02-15 22:36:36 +01:00
|
|
|
|
2022-07-02 17:53:13 +02:00
|
|
|
ImHexApi::System::impl::setGPUVendor(reinterpret_cast<const char *>(glGetString(GL_VENDOR)));
|
2021-04-17 15:46:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WindowSplash::~WindowSplash() {
|
2022-01-18 00:10:10 +01:00
|
|
|
this->exitImGui();
|
|
|
|
this->exitGLFW();
|
2021-04-17 15:46:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::future<bool> WindowSplash::processTasksAsync() {
|
|
|
|
return std::async(std::launch::async, [this] {
|
|
|
|
bool status = true;
|
|
|
|
|
2022-09-19 21:56:43 +02:00
|
|
|
std::atomic<u32> tasksCompleted = 0;
|
2022-09-19 16:54:19 +02:00
|
|
|
for (const auto &[name, task, async] : this->m_tasks) {
|
2022-09-19 21:56:43 +02:00
|
|
|
auto runTask = [&, task = task, name = name] {
|
|
|
|
{
|
|
|
|
std::lock_guard guard(this->m_progressMutex);
|
|
|
|
this->m_currTaskName = name;
|
|
|
|
}
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2022-12-02 12:00:04 +01:00
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
tasksCompleted++;
|
|
|
|
this->m_progress = float(tasksCompleted) / this->m_tasks.size();
|
|
|
|
};
|
|
|
|
|
2022-10-20 08:28:29 +02:00
|
|
|
auto startTime = std::chrono::high_resolution_clock::now();
|
2022-02-01 23:33:42 +01:00
|
|
|
if (!task())
|
|
|
|
status = false;
|
2022-10-20 08:28:29 +02:00
|
|
|
auto endTime = std::chrono::high_resolution_clock::now();
|
|
|
|
|
|
|
|
log::info("Task '{}' finished in {} ms", name, std::chrono::duration_cast<std::chrono::milliseconds>(endTime-startTime).count());
|
2022-09-19 16:54:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (async) {
|
2022-10-04 23:37:48 +02:00
|
|
|
TaskManager::createBackgroundTask(name, [runTask](auto&){ runTask(); });
|
2022-09-19 16:54:19 +02:00
|
|
|
} else {
|
|
|
|
runTask();
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:06:00 +01:00
|
|
|
} catch (std::exception &e) {
|
2022-01-23 21:52:24 +01:00
|
|
|
log::error("Init task '{}' threw an exception: {}", name, e.what());
|
2021-05-21 23:46:36 +02:00
|
|
|
status = false;
|
|
|
|
}
|
2021-04-17 15:46:26 +02:00
|
|
|
}
|
|
|
|
|
2022-09-19 21:56:43 +02:00
|
|
|
while (tasksCompleted < this->m_tasks.size()) {
|
2022-09-19 16:54:19 +02:00
|
|
|
std::this_thread::sleep_for(100ms);
|
2022-09-19 21:56:43 +02:00
|
|
|
}
|
2022-09-19 16:54:19 +02:00
|
|
|
|
2021-04-17 15:46:26 +02:00
|
|
|
// Small extra delay so the last progress step is visible
|
2022-09-19 21:56:43 +02:00
|
|
|
std::this_thread::sleep_for(100ms);
|
2021-04-17 15:46:26 +02:00
|
|
|
|
|
|
|
return status;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-18 20:24:42 +02:00
|
|
|
bool WindowSplash::loop() {
|
2022-02-01 22:09:44 +01:00
|
|
|
auto splash = romfs::get("splash.png");
|
2022-09-18 20:38:45 +02:00
|
|
|
ImGui::Texture splashTexture = ImGui::Texture(reinterpret_cast<const ImU8 *>(splash.data()), splash.size());
|
2021-04-17 15:46:26 +02:00
|
|
|
|
2022-09-18 20:38:45 +02:00
|
|
|
if (!splashTexture.isValid()) {
|
2021-04-20 21:46:48 +02:00
|
|
|
log::fatal("Could not load splash screen image!");
|
2021-04-17 15:46:26 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
2021-04-17 15:46:26 +02:00
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
auto tasksSucceeded = processTasksAsync();
|
2021-04-17 15:46:26 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
auto scale = ImHexApi::System::getGlobalScale();
|
2021-08-31 15:22:00 +02:00
|
|
|
|
2021-04-17 15:46:26 +02:00
|
|
|
while (!glfwWindowShouldClose(this->m_window)) {
|
|
|
|
glfwPollEvents();
|
|
|
|
|
|
|
|
ImGui_ImplOpenGL3_NewFrame();
|
|
|
|
ImGui_ImplGlfw_NewFrame();
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard guard(this->m_progressMutex);
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2021-08-21 00:52:11 +02:00
|
|
|
auto drawList = ImGui::GetForegroundDrawList();
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2022-09-18 20:38:45 +02:00
|
|
|
drawList->AddImage(splashTexture, ImVec2(0, 0), splashTexture.getSize() * scale);
|
2021-08-04 18:57:53 +02:00
|
|
|
|
2021-08-31 15:22:00 +02:00
|
|
|
drawList->AddText(ImVec2(15, 120) * scale, ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("WerWolv 2020 - {0}", &__DATE__[7]).c_str());
|
2021-04-21 19:27:05 +02:00
|
|
|
|
2022-08-02 08:24:46 +02:00
|
|
|
#if defined(DEBUG) && defined(GIT_BRANCH) && defined(GIT_COMMIT_HASH)
|
|
|
|
drawList->AddText(ImVec2(15, 140) * scale, ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("{0} : {1} {2}@{3}", IMHEX_VERSION, ICON_FA_CODE_BRANCH, GIT_BRANCH, GIT_COMMIT_HASH).c_str());
|
|
|
|
#else
|
|
|
|
drawList->AddText(ImVec2(15, 140) * scale, ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("{0}", IMHEX_VERSION).c_str());
|
|
|
|
#endif
|
2021-08-04 18:57:53 +02:00
|
|
|
|
2022-09-18 20:38:45 +02:00
|
|
|
drawList->AddRectFilled(ImVec2(0, splashTexture.getSize().y - 5) * scale, ImVec2(splashTexture.getSize().x * this->m_progress, splashTexture.getSize().y) * scale, 0xFFFFFFFF);
|
2022-10-20 08:28:29 +02:00
|
|
|
drawList->AddText(ImVec2(15, splashTexture.getSize().y - 25) * scale, ImColor(0xFF, 0xFF, 0xFF, 0xFF), hex::format("[{}] {}...", "|/-\\"[ImU32(ImGui::GetTime() * 15) % 4], this->m_currTaskName).c_str());
|
2021-04-17 15:46:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::Render();
|
2023-01-05 16:27:52 +01:00
|
|
|
int displayWidth, displayHeight;
|
|
|
|
glfwGetFramebufferSize(this->m_window, &displayWidth, &displayHeight);
|
|
|
|
glViewport(0, 0, displayWidth, displayHeight);
|
2022-10-01 21:14:49 +02:00
|
|
|
glClearColor(0.00F, 0.00F, 0.00F, 0.00F);
|
2021-04-17 15:46:26 +02:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
|
|
|
|
glfwSwapBuffers(this->m_window);
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
if (tasksSucceeded.wait_for(0s) == std::future_status::ready) {
|
|
|
|
return tasksSucceeded.get();
|
2021-04-17 15:46:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 20:24:42 +02:00
|
|
|
return false;
|
2021-04-17 15:46:26 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
static void centerWindow(GLFWwindow *window) {
|
|
|
|
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
|
|
|
|
if (!monitor)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const GLFWvidmode *mode = glfwGetVideoMode(monitor);
|
|
|
|
if (!mode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int monitorX, monitorY;
|
|
|
|
glfwGetMonitorPos(monitor, &monitorX, &monitorY);
|
|
|
|
|
|
|
|
int windowWidth, windowHeight;
|
|
|
|
glfwGetWindowSize(window, &windowWidth, &windowHeight);
|
|
|
|
|
|
|
|
glfwSetWindowPos(window, monitorX + (mode->width - windowWidth) / 2, monitorY + (mode->height - windowHeight) / 2);
|
|
|
|
}
|
|
|
|
|
2021-04-17 15:46:26 +02:00
|
|
|
void WindowSplash::initGLFW() {
|
2021-08-29 22:17:43 +02:00
|
|
|
glfwSetErrorCallback([](int error, const char *desc) {
|
2021-08-29 22:15:18 +02:00
|
|
|
log::error("GLFW Error [{}] : {}", error, desc);
|
2021-04-17 15:46:26 +02:00
|
|
|
});
|
|
|
|
|
2021-04-20 21:46:48 +02:00
|
|
|
if (!glfwInit()) {
|
|
|
|
log::fatal("Failed to initialize GLFW!");
|
2021-04-17 15:46:26 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
2021-04-17 15:46:26 +02:00
|
|
|
|
2022-08-02 08:24:46 +02:00
|
|
|
#if defined(OS_MACOS)
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
#else
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
|
|
|
|
#endif
|
2021-04-21 20:06:48 +02:00
|
|
|
|
2021-04-18 20:24:42 +02:00
|
|
|
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
2022-08-02 08:24:46 +02:00
|
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
|
2021-04-17 15:46:26 +02:00
|
|
|
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
|
|
|
|
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
|
2022-01-22 22:08:25 +01:00
|
|
|
glfwWindowHint(GLFW_FLOATING, GLFW_FALSE);
|
2022-11-05 21:49:17 +01:00
|
|
|
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_FALSE);
|
2021-04-17 15:46:26 +02:00
|
|
|
|
2022-07-29 17:37:30 +02:00
|
|
|
this->m_window = glfwCreateWindow(1, 400, "Starting ImHex...", nullptr, nullptr);
|
|
|
|
if (this->m_window == nullptr) {
|
|
|
|
log::fatal("Failed to create GLFW window!");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate native scale factor for hidpi displays
|
|
|
|
{
|
2021-09-23 22:56:49 +02:00
|
|
|
float xScale = 0, yScale = 0;
|
2022-07-29 17:37:30 +02:00
|
|
|
glfwGetWindowContentScale(this->m_window, &xScale, &yScale);
|
2021-09-23 22:56:49 +02:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
auto meanScale = std::midpoint(xScale, yScale);
|
2022-11-08 18:09:48 +01:00
|
|
|
if (meanScale <= 0.0F)
|
|
|
|
meanScale = 1.0F;
|
|
|
|
|
|
|
|
#if defined(OS_MACOS)
|
|
|
|
meanScale /= 2.0F;
|
|
|
|
#endif
|
2022-02-01 18:09:40 +01:00
|
|
|
|
|
|
|
ImHexApi::System::impl::setGlobalScale(meanScale);
|
2022-07-29 17:37:30 +02:00
|
|
|
ImHexApi::System::impl::setNativeScale(meanScale);
|
2021-08-04 18:57:53 +02:00
|
|
|
|
2022-07-30 21:25:18 +02:00
|
|
|
log::info("Native scaling set to: {:.1f}", meanScale);
|
2021-04-20 21:46:48 +02:00
|
|
|
}
|
2021-04-17 15:46:26 +02:00
|
|
|
|
2022-07-29 17:37:30 +02:00
|
|
|
glfwSetWindowSize(this->m_window, 640_scaled, 400_scaled);
|
2021-04-17 15:46:26 +02:00
|
|
|
centerWindow(this->m_window);
|
|
|
|
|
|
|
|
glfwMakeContextCurrent(this->m_window);
|
|
|
|
glfwSwapInterval(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WindowSplash::initImGui() {
|
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
GImGui = ImGui::CreateContext();
|
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
|
|
|
|
ImGui_ImplGlfw_InitForOpenGL(this->m_window, true);
|
2022-08-02 13:12:12 +02:00
|
|
|
|
|
|
|
#if defined(OS_MACOS)
|
|
|
|
ImGui_ImplOpenGL3_Init("#version 150");
|
|
|
|
#else
|
|
|
|
ImGui_ImplOpenGL3_Init("#version 130");
|
|
|
|
#endif
|
2021-04-21 19:27:05 +02:00
|
|
|
|
|
|
|
auto &io = ImGui::GetIO();
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ImGui::GetStyle().ScaleAllSizes(ImHexApi::System::getGlobalScale());
|
2021-08-04 18:57:53 +02:00
|
|
|
|
2021-04-21 19:27:05 +02:00
|
|
|
io.Fonts->Clear();
|
|
|
|
|
|
|
|
ImFontConfig cfg;
|
|
|
|
cfg.OversampleH = cfg.OversampleV = 1, cfg.PixelSnapH = true;
|
2022-01-11 23:48:18 +01:00
|
|
|
cfg.SizePixels = 13.0_scaled;
|
2021-04-21 19:27:05 +02:00
|
|
|
io.Fonts->AddFontDefault(&cfg);
|
|
|
|
|
|
|
|
cfg.MergeMode = true;
|
|
|
|
|
|
|
|
ImWchar fontAwesomeRange[] = {
|
2022-01-24 20:53:17 +01:00
|
|
|
ICON_MIN_FA, ICON_MAX_FA, 0
|
2021-04-21 19:27:05 +02:00
|
|
|
};
|
|
|
|
std::uint8_t *px;
|
|
|
|
int w, h;
|
2022-01-11 23:48:18 +01:00
|
|
|
io.Fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, 11.0_scaled, &cfg, fontAwesomeRange);
|
2021-04-21 19:27:05 +02:00
|
|
|
io.Fonts->GetTexDataAsRGBA32(&px, &w, &h);
|
|
|
|
|
|
|
|
// Create new font atlas
|
|
|
|
GLuint tex;
|
|
|
|
glGenTextures(1, &tex);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, tex);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA8, GL_UNSIGNED_INT, px);
|
|
|
|
io.Fonts->SetTexID(reinterpret_cast<ImTextureID>(tex));
|
2022-03-13 17:36:50 +01:00
|
|
|
io.IniFilename = nullptr;
|
2021-04-17 15:46:26 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 00:10:10 +01:00
|
|
|
void WindowSplash::exitGLFW() {
|
2021-04-17 15:46:26 +02:00
|
|
|
glfwDestroyWindow(this->m_window);
|
|
|
|
glfwTerminate();
|
|
|
|
}
|
|
|
|
|
2022-01-18 00:10:10 +01:00
|
|
|
void WindowSplash::exitImGui() {
|
2021-04-17 15:46:26 +02:00
|
|
|
ImGui_ImplOpenGL3_Shutdown();
|
|
|
|
ImGui_ImplGlfw_Shutdown();
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
}
|
|
|
|
|
2021-12-22 13:16:51 +01:00
|
|
|
}
|