10 #include "XPLM/XPLMProcessing.h"
17 inline std::string xswiftbusServiceName() {
return std::string(
"org.swift-project.xswiftbus"); }
23 : m_dbusConnection(std::make_shared<
CDBusConnection>()), m_menu(
CMenu::mainMenu().subMenu(
"xswiftbus"))
25 m_showHideLabelsMenuItem = m_menu.
item(
"Show/Hide Aircraft Labels",
26 [
this] { m_traffic->setDrawingLabels(!m_traffic->isDrawingLabels()); });
27 m_enableDisableXPlaneAtisMenuItem =
28 m_menu.
item(
"Enable/Disable X-Plane ATIS", [
this] { m_atisEnabled.
set(m_atisEnabled.
get() ? 0 : 1); });
29 m_messageWindowSubMenu = m_menu.
subMenu(
"Message Window");
30 m_toggleMessageWindowMenuItem =
31 m_messageWindowSubMenu.
item(
"Show/Hide", [
this] { m_service->toggleMessageBoxVisibility(); });
32 m_popupMessageWindowMenuItem =
33 m_messageWindowSubMenu.
checkableItem(
"Pop up Window on new Message",
true, [
this](
bool checked) {
34 m_popupMessageWindowMenuItem.
setChecked(!checked);
35 m_service->setPopupMessageWindow(!checked);
37 m_disappearMessageWindowMenuItem =
38 m_messageWindowSubMenu.
checkableItem(
"Hide Message Window automatically",
true, [
this](
bool checked) {
39 m_disappearMessageWindowMenuItem.
setChecked(!checked);
40 m_service->setDisappearMessageWindow(!checked);
42 m_planeViewSubMenu = m_menu.
subMenu(
"Follow Plane View");
43 m_planeViewOwnAircraftMenuItem = m_planeViewSubMenu.
item(
56 XPLMRegisterFlightLoopCallback(startServerDeferred, -1,
this);
58 m_atisSaved = m_atisEnabled.
get();
64 m_atisEnabled.
set(m_atisSaved);
66 XPLMUnregisterFlightLoopCallback(flightLoopCallback,
this);
67 m_dbusConnection->close();
69 if (m_dbusThread.joinable()) { m_dbusThread.join(); }
72 void CPlugin::readConfig()
75 const std::string configFilePath =
78 m_pluginConfig.
parse();
79 m_pluginConfig.
print();
82 void CPlugin::startServer()
84 XPLMRegisterFlightLoopCallback(flightLoopCallback, -1,
this);
88 m_service = std::make_unique<CService>(
this);
89 m_traffic = std::make_unique<CTraffic>(
this);
91 m_traffic->setPlaneViewMenu(m_planeViewSubMenu);
93 if (m_pluginConfig.
getDBusMode() == CConfig::DBusP2P)
95 m_dbusP2PServer = std::make_unique<CDBusServer>();
98 std::string listenAddress =
100 if (!m_dbusP2PServer->listen(listenAddress))
102 m_service->addTextMessage(
"xswiftbus startup failed!", 255, 0, 0);
105 m_dbusP2PServer->setDispatcher(&m_dbusDispatcher);
107 m_dbusP2PServer->setNewConnectionFunc([
this](
const std::shared_ptr<CDBusConnection> &conn) {
108 m_dbusConnection = conn;
109 m_dbusConnection->setDispatcher(&m_dbusDispatcher);
110 m_service->setDBusConnection(m_dbusConnection);
111 m_service->registerDBusObjectPath(m_service->InterfaceName(), m_service->ObjectPath());
112 m_traffic->setDBusConnection(m_dbusConnection);
113 m_traffic->registerDBusObjectPath(m_traffic->InterfaceName(), m_traffic->ObjectPath());
119 bool success = m_dbusConnection->connect(CDBusConnection::SessionBus);
127 m_dbusConnection->setDispatcher(&m_dbusDispatcher);
128 m_dbusConnection->requestName(xswiftbusServiceName());
130 m_service->setDBusConnection(m_dbusConnection);
131 m_service->registerDBusObjectPath(m_service->InterfaceName(), m_service->ObjectPath());
132 m_traffic->setDBusConnection(m_dbusConnection);
133 m_traffic->registerDBusObjectPath(m_traffic->InterfaceName(), m_traffic->ObjectPath());
137 const std::string msg =
"xswiftbus " + m_service->getVersionNumber() +
" started.";
139 m_service->addTextMessage(msg, 0, 255, 255);
146 m_service->onAircraftModelChanged();
147 m_service->resetFrameTotals();
153 if (m_service) { m_service->resetFrameTotals(); }
158 if (m_service) { m_service->onSceneryLoaded(); }
161 float CPlugin::startServerDeferred(
float,
float,
int,
void *refcon)
163 auto *plugin =
static_cast<CPlugin *
>(refcon);
164 if (!plugin->m_isRunning)
166 plugin->startServer();
167 plugin->m_isRunning =
true;
168 INFO_LOG(
"xswiftbus plugin started (deferred)");
173 float CPlugin::flightLoopCallback(
float,
float,
int,
void *refcon)
175 auto *plugin =
static_cast<CPlugin *
>(refcon);
177 plugin->m_dbusDispatcher.runOnce();
178 if (plugin->m_service) { plugin->m_service->process(); }
179 if (plugin->m_traffic) { plugin->m_traffic->process(); }
int getDBusPort() const
Get current DBus server port.
void setFilePath(const std::string &filePath)
Set config file path.
void print()
Print the current configuration to the X-Plane log.
DBusMode getDBusMode() const
Get current DBus mode.
void parse()
Load and parse config file.
std::string getDBusAddress() const
Get current DBus server address.
void onSceneryLoaded()
Called by XPluginReceiveMessage when some scenery is loaded.
void onAircraftRepositioned()
Called by XPluginReceiveMessage when the aircraft is positioned at an airport.
void onAircraftModelChanged()
Called by XPluginReceiveMessage when the model is changed.
static const std::string & ownAircraftString()
Returns the own aircraft string to be used as callsign for setFollowedAircraft()
void set(DataRefType d)
Set the value of the dataref (if it is writable)
DataRefType get() const
Get the value of the dataref.
Plugin loaded by X-Plane which publishes a DBus service.
std::string g_xplanePath
Absolute xplane path.
std::string g_sep
Platform specific dir separator.
void initXPlanePath()
Init global xplane path.
#define INFO_LOG(msg)
Logger convenience macros.