From d37ba1d0a343b06ef5ff2dddc87a0b725180e6d9 Mon Sep 17 00:00:00 2001 From: Marko Bencun Date: Mon, 28 Oct 2024 21:50:27 +0100 Subject: [PATCH] frontend/qt: revert d68b673ed, restore custom libserver.h Using the auto-generated server/libsever.h directly does not work after all, at least not when upgrading to Qt 6.2 on Windows with MSVC. cgo uses mingw gcc and produces C99 code in the header. The MSVC compiler does not accept this any longer: ``` typedef _Fcomplex GoComplex64; typedef _Dcomplex GoComplex128; ``` Something about syntax error, I guess _FComplex is not available when compiling this as C++ with MSVC. Not clear why it worked with Qt 5.15 - probably the compiler flags have changed in Qt 6.2. --- frontends/qt/BitBox.pro | 3 +-- frontends/qt/libserver.h | 55 ++++++++++++++++++++++++++++++++++++++++ frontends/qt/main.cpp | 2 +- frontends/qt/webclass.h | 2 +- 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 frontends/qt/libserver.h diff --git a/frontends/qt/BitBox.pro b/frontends/qt/BitBox.pro index 79400f65ac..c408298544 100644 --- a/frontends/qt/BitBox.pro +++ b/frontends/qt/BitBox.pro @@ -56,8 +56,7 @@ SOURCES += \ main.cpp \ filedialog.cpp -# server/libserver.h is generated by building server.go, see the Makefiles in server/. -HEADERS += server/libserver.h webclass.h filedialog.h +HEADERS += libserver.h webclass.h filedialog.h unix:macx { # Those frameworks are needed for Go's http/net packages. diff --git a/frontends/qt/libserver.h b/frontends/qt/libserver.h new file mode 100644 index 0000000000..91d2d7f863 --- /dev/null +++ b/frontends/qt/libserver.h @@ -0,0 +1,55 @@ +#ifndef BACKEND_H +#define BACKEND_H +#include +#include +#include + +// Workaround to be able to use `const char*` as a param type in the exported Go functions. +typedef const char cchar_t; + +typedef void (*pushNotificationsCallback) (const char*); +static void pushNotify(pushNotificationsCallback f, const char* msg) { + f(msg); +} + +typedef void (*responseCallback) (int, const char*); +static void respond(responseCallback f, int queryID, const char* msg) { + f(queryID, msg); +} + +typedef void (*notifyUserCallback) (const char*); +static void notifyUser(notifyUserCallback f, const char* msg) { + f(msg); +} + +typedef char* (*getSaveFilenameCallback) (const char*); +static char* getSaveFilename(getSaveFilenameCallback f, const char* suggestedfilename) { + return f(suggestedfilename); +} + +// equivalent to C.free but suitable for releasing a memory malloc'ed +// in a non-posix portable environment, incompatible with cgo. +// this is especially important on windows where the standard C runtime +// memory management used by cgo and mingw is different from win32 API used +// when compiling C++ code with MSVC. hence, the memory allocated with malloc +// in C++ must always be freed by this function in Go instead of C.free. +typedef void (*cppHeapFree) (void* ptr); +static void customHeapFree(cppHeapFree f, void* ptr) { + f(ptr); +} + +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +extern void backendCall(int queryID, cchar_t* s); +extern void handleURI(cchar_t* uri); +extern void serve(cppHeapFree cppHeapFreeFn, pushNotificationsCallback pushNotificationsFn, responseCallback responseFn, notifyUserCallback notifyUserFn, cchar_t* preferredLocale, getSaveFilenameCallback getSaveFilenameFn); +extern void systemOpen(cchar_t* url); +extern void goLog(cchar_t* msg); + +#ifdef __cplusplus +} +#endif diff --git a/frontends/qt/main.cpp b/frontends/qt/main.cpp index 9914d82245..5a50581156 100644 --- a/frontends/qt/main.cpp +++ b/frontends/qt/main.cpp @@ -43,7 +43,7 @@ #include #include "filedialog.h" -#include "server/libserver.h" +#include "libserver.h" #include "webclass.h" #define APPNAME "BitBoxApp" diff --git a/frontends/qt/webclass.h b/frontends/qt/webclass.h index 625defe90b..fb38f02cfc 100644 --- a/frontends/qt/webclass.h +++ b/frontends/qt/webclass.h @@ -14,7 +14,7 @@ #include -#include "server/libserver.h" +#include "libserver.h" class WebClass : public QObject {