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.appendItem(
26 "Show/Hide Aircraft Labels", [
this] { m_traffic->setDrawingLabels(!m_traffic->isDrawingLabels()); });
27 m_enableDisableXPlaneAtisMenuItem = m_menu.appendItem(
28 "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.appendItem(
"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.appendItem(
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())
75 void CPlugin::readConfig()
78 const std::string configFilePath =
81 m_pluginConfig.
parse();
82 m_pluginConfig.
print();
85 void CPlugin::startServer()
87 XPLMRegisterFlightLoopCallback(flightLoopCallback, -1,
this);
91 m_service = std::make_unique<CService>(
this);
92 m_traffic = std::make_unique<CTraffic>(
this);
94 m_traffic->setPlaneViewMenu(m_planeViewSubMenu);
96 if (m_pluginConfig.
getDBusMode() == CConfig::DBusP2P)
98 m_dbusP2PServer = std::make_unique<CDBusServer>();
101 std::string listenAddress =
103 if (!m_dbusP2PServer->listen(listenAddress))
105 m_service->addTextMessage(
"xswiftbus startup failed!", 255, 0, 0);
108 m_dbusP2PServer->setDispatcher(&m_dbusDispatcher);
110 m_dbusP2PServer->setNewConnectionFunc([
this](
const std::shared_ptr<CDBusConnection> &conn) {
111 m_dbusConnection = conn;
112 m_dbusConnection->setDispatcher(&m_dbusDispatcher);
113 m_service->setDBusConnection(m_dbusConnection);
114 m_service->registerDBusObjectPath(m_service->InterfaceName(), m_service->ObjectPath());
115 m_traffic->setDBusConnection(m_dbusConnection);
116 m_traffic->registerDBusObjectPath(m_traffic->InterfaceName(), m_traffic->ObjectPath());
122 bool success = m_dbusConnection->connect(CDBusConnection::SessionBus);
130 m_dbusConnection->setDispatcher(&m_dbusDispatcher);
131 m_dbusConnection->requestName(xswiftbusServiceName());
133 m_service->setDBusConnection(m_dbusConnection);
134 m_service->registerDBusObjectPath(m_service->InterfaceName(), m_service->ObjectPath());
135 m_traffic->setDBusConnection(m_dbusConnection);
136 m_traffic->registerDBusObjectPath(m_traffic->InterfaceName(), m_traffic->ObjectPath());
140 const std::string msg =
"xswiftbus " + m_service->getVersionNumber() +
" started.";
142 m_service->addTextMessage(msg, 0, 255, 255);
149 m_service->onAircraftModelChanged();
150 m_service->resetFrameTotals();
158 m_service->resetFrameTotals();
166 m_service->onSceneryLoaded();
170 float CPlugin::startServerDeferred(
float,
float,
int,
void *refcon)
172 auto *plugin =
static_cast<CPlugin *
>(refcon);
173 if (!plugin->m_isRunning)
175 plugin->startServer();
176 plugin->m_isRunning =
true;
177 INFO_LOG(
"xswiftbus plugin started (deferred)");
182 float CPlugin::flightLoopCallback(
float,
float,
int,
void *refcon)
184 auto *plugin =
static_cast<CPlugin *
>(refcon);
186 plugin->m_dbusDispatcher.runOnce();
187 if (plugin->m_service)
189 plugin->m_service->process();
191 if (plugin->m_traffic)
193 plugin->m_traffic->process();
int getDBusPort() const
Get current DBus server port.
void setFilePath(const std::string &filePath)
Set config file path.
const std::string & getDBusAddress() const
Get current DBus server address.
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.
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().
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.