diff --git a/src/xbus/main.cpp b/src/xbus/main.cpp index 8f9fe8b30..65ee1156b 100644 --- a/src/xbus/main.cpp +++ b/src/xbus/main.cpp @@ -7,6 +7,11 @@ #define _CRT_SECURE_NO_WARNINGS #include "plugin.h" #include "utils.h" +#include + +#if ! defined(XPLM210) +#define XPLM_MSG_LIVERY_LOADED 108 +#endif QSharedPointer g_qApp; XBus::CPlugin *g_plugin; @@ -40,7 +45,14 @@ PLUGIN_API void XPluginDisable() PLUGIN_API void XPluginReceiveMessage(XPLMPluginID from, long msg, void *param) { - Q_UNUSED(from); - Q_UNUSED(msg); - Q_UNUSED(param); + if (from == XPLM_PLUGIN_XPLANE) + { + if (msg == XPLM_MSG_PLANE_LOADED || msg == XPLM_MSG_LIVERY_LOADED) + { + if (reinterpret_cast(param) == XPLM_USER_AIRCRAFT) + { + g_plugin->onAircraftModelChanged(); + } + } + } } diff --git a/src/xbus/plugin.cpp b/src/xbus/plugin.cpp index df3dcbb3f..a45a1bed6 100644 --- a/src/xbus/plugin.cpp +++ b/src/xbus/plugin.cpp @@ -22,6 +22,16 @@ namespace XBus for (auto &item : m_startServerMenuItems) { item.setEnabled(false); } m_server = new BlackCore::CDBusServer(address, this); + m_service = new CService(this); + m_server->addObject(CService::ObjectPath(), m_service); + } + + void CPlugin::onAircraftModelChanged() + { + if (m_service) + { + m_service->onAircraftModelChanged(); + } } } diff --git a/src/xbus/plugin.h b/src/xbus/plugin.h index b6bea1ec4..065c9defa 100644 --- a/src/xbus/plugin.h +++ b/src/xbus/plugin.h @@ -12,6 +12,7 @@ #define NOMINMAX #include "menus.h" +#include "service.h" #pragma push_macro("interface") #undef interface @@ -32,8 +33,11 @@ namespace XBus public: CPlugin(); + void onAircraftModelChanged(); + private: BlackCore::CDBusServer *m_server = nullptr; + CService *m_service = nullptr; CMenu m_menu; QVector m_startServerMenuItems; diff --git a/src/xbus/service.cpp b/src/xbus/service.cpp new file mode 100644 index 000000000..9a3202d7a --- /dev/null +++ b/src/xbus/service.cpp @@ -0,0 +1,55 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "service.h" +#include +#include + +namespace XBus +{ + + CService::CService(QObject *parent) : QObject(parent) + { + } + + void CService::onAircraftModelChanged() + { + char filename[256]; + char path[512]; + XPLMGetNthAircraftModel(XPLM_USER_AIRCRAFT, filename, path); + emit aircraftModelChanged(path, filename, m_liveryPath.get().c_str()); + } + + int CService::getXPlaneVersionMajor() const + { + int version; + XPLMGetVersions(&version, nullptr, nullptr); + if (version > 5000) { version /= 10; } + return version / 100; + } + + int CService::getXPlaneVersionMinor() const + { + int version; + XPLMGetVersions(&version, nullptr, nullptr); + if (version > 5000) { version /= 10; } + return version % 100; + } + + QString CService::getXPlaneInstallationPath() const + { + char path[512]; + XPLMGetSystemPath(path); + return path; + } + + QString CService::getXPlanePreferencesPath() const + { + char path[512]; + XPLMGetPrefsPath(path); + return path; + } + +} diff --git a/src/xbus/service.h b/src/xbus/service.h new file mode 100644 index 000000000..edd66dceb --- /dev/null +++ b/src/xbus/service.h @@ -0,0 +1,113 @@ +/* Copyright (C) 2013 VATSIM Community / contributors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BLACKSIM_XBUS_SERVICE_H +#define BLACKSIM_XBUS_SERVICE_H + +//! \file + +#include "datarefs.h" +#include + +#define XBUS_SERVICE_INTERFACENAME "net.vatsim.PilotClient.XBus" +#define XBUS_SERVICE_OBJECTPATH "/XBus" + +namespace XBus +{ + + /*! + * XBus service object which is accessible through DBus + */ + class CService : public QObject + { + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", XBUS_SERVICE_INTERFACENAME) + + public: + //! Constructor + CService(QObject *parent); + + //! DBus interface name + static const QString &InterfaceName() + { + static QString s(XBUS_SERVICE_INTERFACENAME); + return s; + } + + //! DBus object path + static const QString &ObjectPath() + { + static QString s(XBUS_SERVICE_OBJECTPATH); + return s; + } + + //! Called by XPluginReceiveMessage when the model changes. + void onAircraftModelChanged(); + + signals: + //! Emitted when the model or livery changes. + void aircraftModelChanged(const QString &path, const QString &filename, const QString &livery); + + public slots: + //! Get major version number + int getXPlaneVersionMajor() const; + + //! Get minor version number + int getXPlaneVersionMinor() const; + + //! Get root of X-Plane install path + QString getXPlaneInstallationPath() const; + + //! Get full path to X-Plane preferences file + QString getXPlanePreferencesPath() const; + + //! Get aircraft latitude in degrees + double getLatitude() const { return m_latitude.get(); } + + //! Get aircraft longitude in degrees + double getLongitude() const { return m_longitude.get(); } + + //! Get aircraft altitude in meters + double getAltitudeMSL() const { return m_elevation.get(); } + + //! Get aircraft height in meters + double getHeightAGL() const { return m_agl.get(); } + + //! Get aircraft groundspeed in meters per second + double getGroundSpeed() const { return m_groundSpeed.get(); } + + //! Get aircraft IAS in knots + double getIndicatedAirspeed() const { return m_indicatedAirspeed.get(); } + + //! Get aircraft TAS in meters per second + double getTrueAirspeed() const { return m_trueAirspeed.get(); } + + //! Get aircraft pitch in degrees above horizon + double getPitch() const { return m_pitch.get(); } + + //! Get aircraft roll in degrees + double getRoll() const { return m_roll.get(); } + + //! Get aircraft true heading in degrees + double getTrueHeading() const { return m_heading.get(); } + + private: + StringDataRef m_liveryPath; + DataRef m_latitude; + DataRef m_longitude; + DataRef m_elevation; + DataRef m_agl; + DataRef m_groundSpeed; + DataRef m_indicatedAirspeed; + DataRef m_trueAirspeed; + DataRef m_pitch; + DataRef m_roll; + DataRef m_heading; + }; + +} + +#endif // guard +