feat: Add a native error message when glfw window creation fails (#1104)

Draft because I want to test it again tomorrow with all OSes, first

---------

Co-authored-by: Nik <werwolv98@gmail.com>
This commit is contained in:
iTrooz
2023-05-27 17:45:41 +02:00
committed by GitHub
parent e578127f67
commit 0ba011dbe1
7 changed files with 76 additions and 3 deletions

View File

@@ -4,6 +4,7 @@
extern "C" {
void errorMessageMacos(const char *message);
void openWebpageMacos(const char *url);
bool isMacosSystemDarkModeEnabled();
float getBackingScaleFactor();

View File

@@ -13,6 +13,11 @@
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
void errorMessageMacos(const char *cMessage) {
CFStringRef strMessage = CFStringCreateWithCString(NULL, cMessage, kCFStringEncodingUTF8);
CFUserNotificationDisplayAlert(0, kCFUserNotificationStopAlertLevel, NULL, NULL, NULL, strMessage, NULL, NULL, NULL, NULL, NULL);
}
void openFile(const char *path);
void openWebpageMacos(const char *url) {

View File

@@ -14,6 +14,8 @@ struct ImGuiSettingsHandler;
namespace hex {
void nativeErrorMessage(const std::string &message);
class Window {
public:
Window();

View File

@@ -1,3 +1,4 @@
#include "window.hpp"
#include "init/splash_window.hpp"
#include <hex/api/imhex_api.hpp>
@@ -32,6 +33,13 @@ using namespace std::literals::chrono_literals;
namespace hex::init {
struct GlfwError {
int errorCode = 0;
std::string desc;
};
GlfwError lastGlfwError;
WindowSplash::WindowSplash() : m_window(nullptr) {
this->initGLFW();
this->initImGui();
@@ -186,8 +194,10 @@ namespace hex::init {
}
void WindowSplash::initGLFW() {
glfwSetErrorCallback([](int error, const char *desc) {
log::error("GLFW Error [{}] : {}", error, desc);
glfwSetErrorCallback([](int errorCode, const char *desc) {
lastGlfwError.errorCode = errorCode;
lastGlfwError.desc = std::string(desc);
log::error("GLFW Error [{}] : {}", errorCode, desc);
});
if (!glfwInit()) {
@@ -216,7 +226,12 @@ namespace hex::init {
// Create the splash screen window
this->m_window = glfwCreateWindow(1, 400, "Starting ImHex...", nullptr, nullptr);
if (this->m_window == nullptr) {
log::fatal("Failed to create GLFW window!");
hex::nativeErrorMessage(hex::format(
"Failed to create GLFW window: [{}] {}.\n"
"You may not have a renderer available.\n"
"The most common cause of this is using a virtual machine\n"
"You may want to try a release artifact ending with 'NoGPU'"
, lastGlfwError.errorCode, lastGlfwError.desc));
exit(EXIT_FAILURE);
}

View File

@@ -17,9 +17,49 @@
#include <unistd.h>
#include <imgui_impl_glfw.h>
#include <string.h>
#include <ranges>
namespace hex {
bool isFileInPath(const std::fs::path &filename) {
auto optPathVar = hex::getEnvironmentVariable("PATH");
if (!optPathVar.has_value()) {
log::error("Could not find variable named PATH");
return false;
}
for (auto dir : std::views::split(optPathVar.value(), ':')) {
if (std::fs::exists(std::fs::path(std::string_view(dir)) / filename)) {
return true;
}
}
return false;
}
void executeCmd(const std::vector<std::string> &argsVector) {
std::vector<char*> cArgsVector;
for (const auto &str : argsVector) {
cArgsVector.push_back(const_cast<char*>(str.c_str()));
}
cArgsVector.push_back(nullptr);
if (fork() == 0) {
execvp(cArgsVector[0], &cArgsVector[0]);
log::error("execvp() failed: {}", strerror(errno));
exit(EXIT_FAILURE);
}
}
void nativeErrorMessage(const std::string &message) {
log::fatal(message);
if (isFileInPath("zenity")) {
executeCmd({"zenity", "--error", "--text", message});
} else if(isFileInPath("notify-send")) {
executeCmd({"notify-send", "-i", "script-error", "Error", message});
} // hopefully one of these commands is installed
}
void Window::initNative() {
// Add plugin library folders to dll search path
for (const auto &path : hex::fs::getDefaultPaths(fs::ImHexPath::Libraries)) {

View File

@@ -17,6 +17,11 @@
namespace hex {
void nativeErrorMessage(const std::string &message) {
log::fatal(message);
errorMessageMacos(message.c_str());
}
void Window::initNative() {
// Add plugin library folders to dll search path
for (const auto &path : hex::fs::getDefaultPaths(fs::ImHexPath::Libraries)) {

View File

@@ -37,6 +37,11 @@ namespace hex {
static float g_titleBarHeight;
static Microsoft::WRL::ComPtr<ITaskbarList4> g_taskbarList;
void nativeErrorMessage(const std::string &message) {
log::fatal(message);
MessageBox(NULL, message.c_str(), "Error", MB_ICONERROR | MB_OK);
}
// Custom Window procedure for receiving OS events
static LRESULT commonWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {