From 0739d5b595be9492c1e574192eba12174111e52c Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 30 Oct 2015 09:16:32 +0100 Subject: also rename protocol -> proto --- proto-sc/CMakeLists.txt | 3 + proto-sc/ftnoir-protocol-sc.rc | 4 + proto-sc/ftnoir_protocol_sc.cpp | 189 ++++++++++++++++++++++++++++++++ proto-sc/ftnoir_protocol_sc.h | 108 ++++++++++++++++++ proto-sc/ftnoir_protocol_sc_dialog.cpp | 35 ++++++ proto-sc/ftnoir_sccontrols.ui | 72 ++++++++++++ proto-sc/images/fsx.png | Bin 0 -> 813 bytes proto-sc/images/fsx1.png | Bin 0 -> 1920 bytes proto-sc/sc-protocol.qrc | 5 + proto-sc/scserver.manifest | 13 +++ proto-sc/scserver_acceleration.manifest | 13 +++ proto-sc/scserver_sp2.manifest | 13 +++ 12 files changed, 455 insertions(+) create mode 100644 proto-sc/CMakeLists.txt create mode 100644 proto-sc/ftnoir-protocol-sc.rc create mode 100644 proto-sc/ftnoir_protocol_sc.cpp create mode 100644 proto-sc/ftnoir_protocol_sc.h create mode 100644 proto-sc/ftnoir_protocol_sc_dialog.cpp create mode 100644 proto-sc/ftnoir_sccontrols.ui create mode 100644 proto-sc/images/fsx.png create mode 100644 proto-sc/images/fsx1.png create mode 100644 proto-sc/sc-protocol.qrc create mode 100644 proto-sc/scserver.manifest create mode 100644 proto-sc/scserver_acceleration.manifest create mode 100644 proto-sc/scserver_sp2.manifest (limited to 'proto-sc') diff --git a/proto-sc/CMakeLists.txt b/proto-sc/CMakeLists.txt new file mode 100644 index 00000000..bd83e944 --- /dev/null +++ b/proto-sc/CMakeLists.txt @@ -0,0 +1,3 @@ +if(WIN32) + opentrack_boilerplate(opentrack-proto-simconnect) +endif() diff --git a/proto-sc/ftnoir-protocol-sc.rc b/proto-sc/ftnoir-protocol-sc.rc new file mode 100644 index 00000000..c89eb9a7 --- /dev/null +++ b/proto-sc/ftnoir-protocol-sc.rc @@ -0,0 +1,4 @@ +#include +142 RT_MANIFEST scserver.manifest +143 RT_MANIFEST scserver_sp2.manifest +144 RT_MANIFEST scserver_acceleration.manifest \ No newline at end of file diff --git a/proto-sc/ftnoir_protocol_sc.cpp b/proto-sc/ftnoir_protocol_sc.cpp new file mode 100644 index 00000000..0c6cb486 --- /dev/null +++ b/proto-sc/ftnoir_protocol_sc.cpp @@ -0,0 +1,189 @@ +/* Homepage http://facetracknoir.sourceforge.net/home/default.htm * + * * + * ISC License (ISC) * + * * + * Copyright (c) 2015, Wim Vriend + * Copyright (c) 2014, Stanislaw Halik + * * + * Permission to use, copy, modify, and/or distribute this software for any * + * purpose with or without fee is hereby granted, provided that the above * + * copyright notice and this permission notice appear in all copies. * + */ +#include "ftnoir_protocol_sc.h" +#include "opentrack/plugin-api.hpp" + +FTNoIR_Protocol::FTNoIR_Protocol() : should_stop(false), hSimConnect(nullptr) +{ +} + +FTNoIR_Protocol::~FTNoIR_Protocol() +{ + should_stop = true; + wait(); +} + +void FTNoIR_Protocol::run() +{ + HANDLE event = CreateEvent(NULL, FALSE, FALSE, nullptr); + + if (event == nullptr) + { + qDebug() << "simconnect: event create" << GetLastError(); + return; + } + + while (!should_stop) + { + if (SUCCEEDED(simconnect_open(&hSimConnect, "opentrack", NULL, 0, event, 0))) + { + simconnect_subscribetosystemevent(hSimConnect, 0, "Frame"); + + while (!should_stop) + { + if (WaitForSingleObject(event, 10) == WAIT_OBJECT_0) + { + if (FAILED(simconnect_calldispatch(hSimConnect, processNextSimconnectEvent, reinterpret_cast(this)))) + break; + } + } + + (void) simconnect_close(hSimConnect); + } + + if (!should_stop) + Sleep(100); + } + + CloseHandle(event); +} + +void FTNoIR_Protocol::pose( const double *headpose ) { + virtSCRotX = -headpose[Pitch]; // degrees + virtSCRotY = headpose[Yaw]; + virtSCRotZ = headpose[Roll]; + + virtSCPosX = headpose[TX]/100.f; // cm to meters + virtSCPosY = headpose[TY]/100.f; + virtSCPosZ = -headpose[TZ]/100.f; +} + +#ifdef __GNUC__ +# pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif + +class ActivationContext { +public: + ActivationContext(const int resid) : ok(false) { + hactctx = INVALID_HANDLE_VALUE; + actctx_cookie = 0; + ACTCTXA actx = {0}; + actx.cbSize = sizeof(ACTCTXA); + actx.lpResourceName = MAKEINTRESOURCEA(resid); + actx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID; +#ifdef _MSC_VER +# define PREFIX "" +#else +# define PREFIX "lib" +#endif + QString path = QCoreApplication::applicationDirPath() + "/" PREFIX "opentrack-proto-simconnect.dll"; + QByteArray name = QFile::encodeName(path); + actx.lpSource = name.constData(); + hactctx = CreateActCtxA(&actx); + actctx_cookie = 0; + if (hactctx != INVALID_HANDLE_VALUE) { + if (!ActivateActCtx(hactctx, &actctx_cookie)) { + qDebug() << "SC: can't set win32 activation context" << GetLastError(); + ReleaseActCtx(hactctx); + hactctx = INVALID_HANDLE_VALUE; + } + else + ok = true; + } else { + qDebug() << "SC: can't create win32 activation context" << GetLastError(); + } + } + ~ActivationContext() { + if (hactctx != INVALID_HANDLE_VALUE) + { + DeactivateActCtx(0, actctx_cookie); + ReleaseActCtx(hactctx); + } + } + bool is_ok() { return ok; } +private: + ULONG_PTR actctx_cookie; + HANDLE hactctx; + bool ok; +}; + +bool FTNoIR_Protocol::correct() +{ + if (!SCClientLib.isLoaded()) + { + ActivationContext ctx(142 + static_cast(s.sxs_manifest)); + + if (ctx.is_ok()) + { + SCClientLib.setFileName("SimConnect.dll"); + if (!SCClientLib.load()) { + qDebug() << "SC load" << SCClientLib.errorString(); + return false; + } + } + else + return false; + } + + simconnect_open = (importSimConnect_Open) SCClientLib.resolve("SimConnect_Open"); + if (simconnect_open == NULL) { + qDebug() << "FTNoIR_Protocol::correct() says: SimConnect_Open function not found in DLL!"; + return false; + } + simconnect_set6DOF = (importSimConnect_CameraSetRelative6DOF) SCClientLib.resolve("SimConnect_CameraSetRelative6DOF"); + if (simconnect_set6DOF == NULL) { + qDebug() << "FTNoIR_Protocol::correct() says: SimConnect_CameraSetRelative6DOF function not found in DLL!"; + return false; + } + simconnect_close = (importSimConnect_Close) SCClientLib.resolve("SimConnect_Close"); + if (simconnect_close == NULL) { + qDebug() << "FTNoIR_Protocol::correct() says: SimConnect_Close function not found in DLL!"; + return false; + } + + simconnect_calldispatch = (importSimConnect_CallDispatch) SCClientLib.resolve("SimConnect_CallDispatch"); + if (simconnect_calldispatch == NULL) { + qDebug() << "FTNoIR_Protocol::correct() says: SimConnect_CallDispatch function not found in DLL!"; + return false; + } + + simconnect_subscribetosystemevent = (importSimConnect_SubscribeToSystemEvent) SCClientLib.resolve("SimConnect_SubscribeToSystemEvent"); + if (simconnect_subscribetosystemevent == NULL) { + qDebug() << "FTNoIR_Protocol::correct() says: SimConnect_SubscribeToSystemEvent function not found in DLL!"; + return false; + } + + start(); + + return true; +} + +void FTNoIR_Protocol::handle() +{ + (void) simconnect_set6DOF(hSimConnect, virtSCPosX, virtSCPosY, virtSCPosZ, virtSCRotX, virtSCRotZ, virtSCRotY); +} + +void CALLBACK FTNoIR_Protocol::processNextSimconnectEvent(SIMCONNECT_RECV* pData, DWORD, void *self_) +{ + FTNoIR_Protocol& self = *reinterpret_cast(self_); + + switch(pData->dwID) + { + default: + break; + case SIMCONNECT_RECV_ID_EVENT_FRAME: + self.handle(); + break; + } +} + +OPENTRACK_DECLARE_PROTOCOL(FTNoIR_Protocol, SCControls, FTNoIR_ProtocolDll) diff --git a/proto-sc/ftnoir_protocol_sc.h b/proto-sc/ftnoir_protocol_sc.h new file mode 100644 index 00000000..671a3500 --- /dev/null +++ b/proto-sc/ftnoir_protocol_sc.h @@ -0,0 +1,108 @@ +/* Homepage http://facetracknoir.sourceforge.net/home/default.htm * + * * + * ISC License (ISC) * + * * + * Copyright (c) 2015, Wim Vriend * + * Copyright (c) 2014, Stanislaw Halik * + * * + * Permission to use, copy, modify, and/or distribute this software for any * + * purpose with or without fee is hereby granted, provided that the above * + * copyright notice and this permission notice appear in all copies. * + */ +#pragma once +#include "opentrack/plugin-api.hpp" + +#include "ui_ftnoir_sccontrols.h" +#include +#include +#include +#include +#include +#include +#include +#include "opentrack/options.hpp" +using namespace options; +#include + +struct settings : opts { + value sxs_manifest; + settings() : + opts("proto-simconnect"), + sxs_manifest(b, "sxs-manifest-version", 0) + {} +}; + +class FTNoIR_Protocol : public IProtocol, private QThread +{ +public: + FTNoIR_Protocol(); + ~FTNoIR_Protocol() override; + bool correct(); + void pose(const double* headpose); + void handle(); + QString game_name() { + return "FS2004/FSX"; + } +private: + enum { SIMCONNECT_RECV_ID_EVENT_FRAME = 7 }; + + #pragma pack(push, 1) + struct SIMCONNECT_RECV + { + DWORD dwSize; + DWORD dwVersion; + DWORD dwID; + }; + #pragma pack(pop) + + typedef void (CALLBACK *DispatchProc)(SIMCONNECT_RECV*, DWORD, void*); + + typedef HRESULT (WINAPI *importSimConnect_Open)(HANDLE * phSimConnect, LPCSTR szName, HWND hWnd, DWORD UserEventWin32, HANDLE hEventHandle, DWORD ConfigIndex); + typedef HRESULT (WINAPI *importSimConnect_Close)(HANDLE hSimConnect); + typedef HRESULT (WINAPI *importSimConnect_CameraSetRelative6DOF)(HANDLE hSimConnect, float fDeltaX, float fDeltaY, float fDeltaZ, float fPitchDeg, float fBankDeg, float fHeadingDeg); + typedef HRESULT (WINAPI *importSimConnect_CallDispatch)(HANDLE hSimConnect, DispatchProc pfcnDispatch, void * pContext); + typedef HRESULT (WINAPI *importSimConnect_SubscribeToSystemEvent)(HANDLE hSimConnect, DWORD EventID, const char * SystemEventName); + + void run() override; + volatile bool should_stop; + + volatile float virtSCPosX; + volatile float virtSCPosY; + volatile float virtSCPosZ; + volatile float virtSCRotX; + volatile float virtSCRotY; + volatile float virtSCRotZ; + + importSimConnect_Open simconnect_open; + importSimConnect_Close simconnect_close; + importSimConnect_CameraSetRelative6DOF simconnect_set6DOF; + importSimConnect_CallDispatch simconnect_calldispatch; + importSimConnect_SubscribeToSystemEvent simconnect_subscribetosystemevent; + + HANDLE hSimConnect; + static void CALLBACK processNextSimconnectEvent(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext); + settings s; + QLibrary SCClientLib; +}; + +class SCControls: public IProtocolDialog +{ + Q_OBJECT +public: + SCControls(); + void register_protocol(IProtocol *) {} + void unregister_protocol() {} +private: + Ui::UICSCControls ui; + settings s; +private slots: + void doOK(); + void doCancel(); +}; + +class FTNoIR_ProtocolDll : public Metadata +{ +public: + QString name() { return QString("Microsoft FSX SimConnect"); } + QIcon icon() { return QIcon(":/images/fsx.png"); } +}; diff --git a/proto-sc/ftnoir_protocol_sc_dialog.cpp b/proto-sc/ftnoir_protocol_sc_dialog.cpp new file mode 100644 index 00000000..7c2ecfd4 --- /dev/null +++ b/proto-sc/ftnoir_protocol_sc_dialog.cpp @@ -0,0 +1,35 @@ +/* Homepage http://facetracknoir.sourceforge.net/home/default.htm * + * * + * ISC License (ISC) * + * * + * Copyright (c) 2015, Wim Vriend * + * * + * Permission to use, copy, modify, and/or distribute this software for any * + * purpose with or without fee is hereby granted, provided that the above * + * copyright notice and this permission notice appear in all copies. * + */ +#include "ftnoir_protocol_sc.h" +#include +#include "opentrack/plugin-api.hpp" + +SCControls::SCControls() +{ + ui.setupUi( this ); + + // Connect Qt signals to member-functions + connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(doOK())); + connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(doCancel())); + + tie_setting(s.sxs_manifest, ui.comboBox); +} + +void SCControls::doOK() { + s.b->save(); + this->close(); +} + +void SCControls::doCancel() { + s.b->reload(); + close(); +} + diff --git a/proto-sc/ftnoir_sccontrols.ui b/proto-sc/ftnoir_sccontrols.ui new file mode 100644 index 00000000..5b2fd291 --- /dev/null +++ b/proto-sc/ftnoir_sccontrols.ui @@ -0,0 +1,72 @@ + + + UICSCControls + + + Qt::NonModal + + + + 0 + 0 + 290 + 79 + + + + SimConnect settings FaceTrackNoIR + + + + images/FaceTrackNoIR.pngimages/FaceTrackNoIR.png + + + Qt::LeftToRight + + + false + + + + + + FSX version + + + + + + + + SP1 + + + + + SP2 + + + + + Acceleration + + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + startEngineClicked() + stopEngineClicked() + cameraSettingsClicked() + + diff --git a/proto-sc/images/fsx.png b/proto-sc/images/fsx.png new file mode 100644 index 00000000..16b072a1 Binary files /dev/null and b/proto-sc/images/fsx.png differ diff --git a/proto-sc/images/fsx1.png b/proto-sc/images/fsx1.png new file mode 100644 index 00000000..a1f0f188 Binary files /dev/null and b/proto-sc/images/fsx1.png differ diff --git a/proto-sc/sc-protocol.qrc b/proto-sc/sc-protocol.qrc new file mode 100644 index 00000000..127d5180 --- /dev/null +++ b/proto-sc/sc-protocol.qrc @@ -0,0 +1,5 @@ + + + images/fsx.png + + diff --git a/proto-sc/scserver.manifest b/proto-sc/scserver.manifest new file mode 100644 index 00000000..d342cfda --- /dev/null +++ b/proto-sc/scserver.manifest @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/proto-sc/scserver_acceleration.manifest b/proto-sc/scserver_acceleration.manifest new file mode 100644 index 00000000..06459587 --- /dev/null +++ b/proto-sc/scserver_acceleration.manifest @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/proto-sc/scserver_sp2.manifest b/proto-sc/scserver_sp2.manifest new file mode 100644 index 00000000..3020d16c --- /dev/null +++ b/proto-sc/scserver_sp2.manifest @@ -0,0 +1,13 @@ + + + + + + + + + + + + + -- cgit v1.2.3