-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ufc-plugin: Wrap plugin code in a class
- Loading branch information
Showing
4 changed files
with
109 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,98 @@ | ||
|
||
#include <XPLMDefs.h> | ||
#include <XPLMProcessing.h> | ||
|
||
#include <ufc/flightconnector.h> | ||
|
||
#include "xpplugindatasource.h" | ||
#include "ufcplugin.h" | ||
|
||
#include <XPLMProcessing.h> | ||
|
||
using namespace std; | ||
using namespace UFC; | ||
|
||
FlightConnector* g_flightConnector = nullptr; | ||
shared_ptr<XPPluginDataSource> g_dataSource; | ||
|
||
float deferredInit(float elapsedMe, float elapsedSim, int counter, void * refcon); | ||
|
||
class XPLogPrinter : public LogPrinter | ||
{ | ||
public: | ||
void printf(const char* message, ...) override | ||
{ | ||
va_list va; | ||
va_start(va, message); | ||
|
||
char buf[4096]; | ||
vsnprintf(buf, 4094, message, va); | ||
XPLMDebugString(buf); | ||
|
||
va_end(va); | ||
} | ||
}; | ||
UFCPlugin g_ufcPlugin; | ||
|
||
PLUGIN_API int XPluginStart(char* outName, char* outSig, char* outDesc) | ||
int UFCPlugin::start(char* outName, char* outSig, char* outDesc) | ||
{ | ||
XPLogPrinter* logPrinter = new XPLogPrinter(); | ||
Logger::setLogPrinter(logPrinter); | ||
setLogPrinter(&m_logPrinter); | ||
|
||
fprintf(stderr, "UFC: XPluginStart: Here!\n"); | ||
strcpy(outName, "Universal Flight Connector"); | ||
strcpy(outSig, "com.geekprojects.ufc.plugin"); | ||
strcpy(outDesc, "Universal Flight Connector"); | ||
|
||
logPrinter->printf("UFC: XPluginStart: Creating Flight Connector...\n"); | ||
g_flightConnector = new FlightConnector(); | ||
g_flightConnector->disableExitHandler(); | ||
log(DEBUG, "UFC: XPluginStart: Creating Flight Connector..."); | ||
m_flightConnector = new FlightConnector(); | ||
m_flightConnector->disableExitHandler(); | ||
|
||
logPrinter->printf("UFC: XPluginStart: Creating Data Source...\n"); | ||
g_dataSource = make_shared<XPPluginDataSource>(g_flightConnector); | ||
g_flightConnector->setDataSource(g_dataSource); | ||
log(DEBUG, "UFC: XPluginStart: Creating Data Source..."); | ||
m_dataSource = make_shared<XPPluginDataSource>(m_flightConnector); | ||
m_flightConnector->setDataSource(m_dataSource); | ||
|
||
logPrinter->printf("UFC: XPluginStart: Initialising Flight Connector...\n"); | ||
g_flightConnector->init(); | ||
log(DEBUG, "UFC: XPluginStart: Initialising Flight Connector..."); | ||
m_flightConnector->init(); | ||
|
||
if (!g_flightConnector->getDevices().empty()) | ||
if (!m_flightConnector->getDevices().empty()) | ||
{ | ||
XPLMRegisterFlightLoopCallback(deferredInit, -1, nullptr); | ||
XPLMRegisterFlightLoopCallback(initCallback, -1, this); | ||
} | ||
else | ||
{ | ||
logPrinter->printf("UFC: No devices found. Not Starting."); | ||
log(WARN, "UFC: No devices found. Not Starting."); | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
PLUGIN_API void XPluginStop(void) | ||
void UFCPlugin::stop() | ||
{ | ||
if (g_flightConnector != nullptr) | ||
if (m_flightConnector != nullptr) | ||
{ | ||
g_flightConnector->stop(); | ||
m_flightConnector->stop(); | ||
|
||
delete m_flightConnector; | ||
m_flightConnector = nullptr; | ||
} | ||
} | ||
|
||
PLUGIN_API void XPluginDisable(void) | ||
int UFCPlugin::enable() | ||
{ | ||
|
||
return 1; | ||
} | ||
|
||
PLUGIN_API int XPluginEnable(void) | ||
void UFCPlugin::disable() | ||
{ | ||
return 1; | ||
} | ||
|
||
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFrom, int inMsg, void * inParam) | ||
float UFCPlugin::initCallback(float elapsedMe, float elapsedSim, int counter, void * refcon) | ||
{ | ||
return ((UFCPlugin*)refcon)->init(elapsedMe, elapsedSim, counter); | ||
} | ||
|
||
float deferredInit(float elapsedMe, float elapsedSim, int counter, void * refcon) | ||
float UFCPlugin::init(float elapsedMe, float elapsedSim, int counter) | ||
{ | ||
g_dataSource->connect(); | ||
g_flightConnector->start(); | ||
m_dataSource->connect(); | ||
m_flightConnector->start(); | ||
return 0; | ||
} | ||
|
||
PLUGIN_API int XPluginStart(char* outName, char* outSig, char* outDesc) | ||
{ | ||
return g_ufcPlugin.start(outName, outSig, outDesc); | ||
} | ||
|
||
PLUGIN_API void XPluginStop() | ||
{ | ||
g_ufcPlugin.stop(); | ||
} | ||
|
||
PLUGIN_API int XPluginEnable() | ||
{ | ||
return g_ufcPlugin.enable(); | ||
} | ||
|
||
PLUGIN_API void XPluginDisable() | ||
{ | ||
g_ufcPlugin.disable(); | ||
} | ||
|
||
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFrom, int inMsg, void * inParam) | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// Created by Ian Parker on 19/11/2024. | ||
// | ||
|
||
#ifndef UFCPLUGIN_H | ||
#define UFCPLUGIN_H | ||
|
||
#include <XPLMUtilities.h> | ||
|
||
#include <ufc/flightconnector.h> | ||
|
||
class XPPluginDataSource; | ||
|
||
class XPLogPrinter : public UFC::LogPrinter | ||
{ | ||
public: | ||
virtual ~XPLogPrinter() = default; | ||
|
||
void printf(const char* message, ...) override | ||
{ | ||
va_list va; | ||
va_start(va, message); | ||
|
||
char buf[4096]; | ||
vsnprintf(buf, 4094, message, va); | ||
XPLMDebugString(buf); | ||
|
||
va_end(va); | ||
} | ||
}; | ||
|
||
class UFCPlugin : public UFC::Logger | ||
{ | ||
private: | ||
XPLogPrinter m_logPrinter; | ||
UFC::FlightConnector* m_flightConnector = nullptr; | ||
std::shared_ptr<XPPluginDataSource> m_dataSource = nullptr; | ||
|
||
static float initCallback(float elapsedMe, float elapsedSim, int counter, void * refcon); | ||
float init(float elapsedMe, float elapsedSim, int counter); | ||
|
||
public: | ||
UFCPlugin() : Logger("UFCPlugin") {} | ||
|
||
int start(char* outName, char* outSig, char* outDesc); | ||
void stop(); | ||
int enable(); | ||
void disable(); | ||
}; | ||
|
||
#endif //UFCPLUGIN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters