Skip to content

Commit

Permalink
frontend/qt: revert d68b673, restore custom libserver.h
Browse files Browse the repository at this point in the history
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.

The custom libserver.h contains the same as the auto-generated one,
but reduced to the declarations we actually need, not all the Go C
preamble stuff that causes the compat problems with MSVC.
  • Loading branch information
benma committed Oct 28, 2024
1 parent 56e7ba5 commit e9c0c33
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
3 changes: 1 addition & 2 deletions frontends/qt/BitBox.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
55 changes: 55 additions & 0 deletions frontends/qt/libserver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef BACKEND_H
#define BACKEND_H
#include <string.h>
#include <stdint.h>
#include <stdlib.h>

// 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
2 changes: 1 addition & 1 deletion frontends/qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include <string.h>

#include "filedialog.h"
#include "server/libserver.h"
#include "libserver.h"
#include "webclass.h"

#define APPNAME "BitBoxApp"
Expand Down
2 changes: 1 addition & 1 deletion frontends/qt/webclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include <QApplication>

#include "server/libserver.h"
#include "libserver.h"

class WebClass : public QObject
{
Expand Down

0 comments on commit e9c0c33

Please sign in to comment.