From b0a4f70fa9e035b3afd0330ef41aedfd5e6c488f Mon Sep 17 00:00:00 2001 From: ts-korhonen Date: Thu, 2 Dec 2021 19:35:08 +0200 Subject: [PATCH] Tweaks to support QT on Windows --- src/qt/qt_main.cpp | 1 + src/qt/qt_mainwindow.cpp | 4 ++- src/qt/qt_platform.cpp | 66 ++++++++++++++++++++++++++++++++++++++-- src/qt/qt_ui.cpp | 4 +-- 4 files changed, 70 insertions(+), 5 deletions(-) diff --git a/src/qt/qt_main.cpp b/src/qt/qt_main.cpp index f011fd9f7..536370371 100644 --- a/src/qt/qt_main.cpp +++ b/src/qt/qt_main.cpp @@ -11,6 +11,7 @@ #include +#define SDL_MAIN_HANDLED #include "SDL.h" #include "SDL_mutex.h" #include "SDL_timer.h" diff --git a/src/qt/qt_mainwindow.cpp b/src/qt/qt_mainwindow.cpp index 8e5b5a450..09f2614e2 100644 --- a/src/qt/qt_mainwindow.cpp +++ b/src/qt/qt_mainwindow.cpp @@ -622,7 +622,9 @@ static std::array& selected_keycode = x11_to_xt_base; uint16_t x11_keycode_to_keysym(uint32_t keycode) { -#ifdef __APPLE__ +#if defined(Q_OS_WINDOWS) + return keycode & 0xFFFF; +#elif defined(__APPLE__) return darwin_to_xt[keycode]; #else static Display* x11display = nullptr; diff --git a/src/qt/qt_platform.cpp b/src/qt/qt_platform.cpp index 6be563814..357d9e0c3 100644 --- a/src/qt/qt_platform.cpp +++ b/src/qt/qt_platform.cpp @@ -44,6 +44,9 @@ private: }; extern "C" { +#ifdef Q_OS_WINDOWS +#include +#endif #include <86box/86box.h> #include <86box/device.h> #include <86box/gameport.h> @@ -87,7 +90,11 @@ do_stop(void) void plat_get_exe_name(char *s, int size) { - CharPointer(s, size) = QCoreApplication::applicationFilePath().toUtf8(); + QByteArray exepath_temp = QCoreApplication::applicationDirPath().toLocal8Bit(); + + memcpy(s, exepath_temp.data(), std::min(exepath_temp.size(),size)); + + plat_path_slash(s); } uint32_t @@ -140,6 +147,8 @@ plat_fopen(const char *path, const char *mode) return nullptr; } */ + +/* Not sure if any this is necessary, fopen seems to work on Windows -Manaatti #ifdef Q_OS_WINDOWS wchar_t *pathw, *modew; int len; @@ -165,8 +174,9 @@ plat_fopen(const char *path, const char *mode) } #endif #ifdef Q_OS_UNIX +*/ return fopen(path, mode); -#endif +//#endif } FILE * @@ -216,11 +226,23 @@ plat_get_extension(char *s) char * plat_get_filename(char *s) { +#ifdef Q_OS_WINDOWS + int c = strlen(s) - 1; + + while (c > 0) { + if (s[c] == '/' || s[c] == '\\') + return(&s[c+1]); + c--; + } + + return(s); +#else auto idx = QByteArray::fromRawData(s, strlen(s)).lastIndexOf(QDir::separator().toLatin1()); if (idx >= 0) { return s+idx+1; } return s; +#endif } int @@ -292,7 +314,11 @@ plat_mmap(size_t size, uint8_t executable) void plat_munmap(void *ptr, size_t size) { +#if defined Q_OS_WINDOWS + VirtualFree(ptr, 0, MEM_RELEASE); +#else munmap(ptr, size); +#endif } void @@ -391,3 +417,39 @@ void endblit() } } + +#ifdef Q_OS_WINDOWS +size_t mbstoc16s(uint16_t dst[], const char src[], int len) +{ + if (src == NULL) return 0; + if (len < 0) return 0; + + size_t ret = MultiByteToWideChar(CP_UTF8, 0, src, -1, reinterpret_cast(dst), dst == NULL ? 0 : len); + + if (!ret) { + return -1; + } + + return ret; +} + +size_t c16stombs(char dst[], const uint16_t src[], int len) +{ + if (src == NULL) return 0; + if (len < 0) return 0; + + size_t ret = WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast(src), -1, dst, dst == NULL ? 0 : len, NULL, NULL); + + if (!ret) { + return -1; + } + + return ret; +} +#endif + +int +plat_chdir(char *path) +{ + return QDir::setCurrent(QString(path)) ? 0 : -1; +} diff --git a/src/qt/qt_ui.cpp b/src/qt/qt_ui.cpp index c4de70282..9025d95da 100644 --- a/src/qt/qt_ui.cpp +++ b/src/qt/qt_ui.cpp @@ -62,8 +62,8 @@ void plat_mouse_capture(int on) { } int ui_msgbox_header(int flags, void *header, void* message) { - if (header <= (void*)7168) header = plat_get_string(reinterpret_cast(header)); - if (message <= (void*)7168) message = plat_get_string(reinterpret_cast(message)); + if (header <= (void*)7168) header = plat_get_string((uintptr_t)header); + if (message <= (void*)7168) message = plat_get_string((uintptr_t)message); auto hdr = QString::fromWCharArray(reinterpret_cast(header)); auto msg = QString::fromWCharArray(reinterpret_cast(message));