Skip to content

Commit

Permalink
xplane.cpp: use refercnes, do a proper shutdown when start a new flight
Browse files Browse the repository at this point in the history
Signed-off-by: Norbert Takacs <[email protected]>
  • Loading branch information
norberttak committed Jan 8, 2023
1 parent 2d8b8d7 commit 6ab6b44
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/XPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ int init_and_start_xpanel_plugin();

Configuration config;
std::vector<Device*> devices;
bool plugin_already_initialized = false;
const float FLIGHT_LOOP_TIME_PERIOD = 0.2f;
const float ERROR_DISPLAY_TIME_PERIOD = 2.0f;

Expand Down Expand Up @@ -122,29 +123,29 @@ PLUGIN_API int XPluginEnable(void)

float flight_loop_callback(float, float, int, void*)
{
for (const auto& it : config.device_configs)
for (const auto &it : config.device_configs)
{
// check and set LED states
for (const auto& triggers : it.light_triggers)
for (const auto &triggers : it.light_triggers)
{
for (auto trigger : triggers.second)
for (auto &trigger : triggers.second)
{
trigger->evaluate_and_store_action();
}
}
// check and set 7 segment display states
for (const auto& display : it.multi_displays)
for (const auto &display : it.multi_displays)
{
display.second->evaluate_and_store_dataref_value();
}

for (const auto& display : it.generic_displays)
for (const auto &display : it.generic_displays)
{
display.second->evaluate_and_store_dataref_value();
}

// update the FIP devices
for (auto screen : it.fip_screens)
for (auto &screen : it.fip_screens)
{
screen.second->evaluate_and_store_screen_action();
}
Expand Down Expand Up @@ -175,7 +176,7 @@ void stop_and_clear_xpanel_plugin()
XPLMUnregisterFlightLoopCallback(flight_loop_callback, NULL);
LuaHelper::get_instace()->close();

for (auto dev : devices)
for (auto &dev : devices)
{
if (dev != NULL)
{
Expand All @@ -186,6 +187,7 @@ void stop_and_clear_xpanel_plugin()
devices.clear();
config.clear();
ActionQueue::get_instance()->clear_all_actions();
plugin_already_initialized = false;
}

std::filesystem::path absolute_path(std::string aircraft_path, std::string file_name)
Expand Down Expand Up @@ -262,7 +264,7 @@ int init_and_start_xpanel_plugin(void)

Device* device;

for (auto& it : config.device_configs)
for (auto &it : config.device_configs)
{
switch (it.device_type) {
case DeviceType::SAITEK_MULTI:
Expand Down Expand Up @@ -355,6 +357,7 @@ int init_and_start_xpanel_plugin(void)
}

XPLMRegisterFlightLoopCallback(flight_loop_callback, FLIGHT_LOOP_TIME_PERIOD, NULL);
plugin_already_initialized = true;
Logger(TLogLevel::logINFO) << "successful init and start plugin" << std::endl;
return EXIT_SUCCESS;
}
Expand All @@ -365,14 +368,18 @@ PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFrom, int inMsg, void*)
{
switch (inMsg) {
case XPLM_MSG_AIRPORT_LOADED:
Logger(TLogLevel::logTRACE) << "XPLM_MSG_AIRPORT_LOADED message received" << std::endl;

if (plugin_already_initialized)
stop_and_clear_xpanel_plugin();

if (init_and_start_xpanel_plugin() != EXIT_SUCCESS)
{
Logger(TLogLevel::logERROR) << "error during plugin init and start" << std::endl;
}

break;
case XPLM_MSG_PLANE_CRASHED:
case XPLM_MSG_PLANE_UNLOADED:
Logger(TLogLevel::logDEBUG) << "XPLM_MSG_PLANE_CRASHED or XPLM_MSG_PLANE_UNLOADED message recived" << std::endl;
Logger(TLogLevel::logINFO) << "XPLM_MSG_PLANE_CRASHED or XPLM_MSG_PLANE_UNLOADED message received" << std::endl;
break;
default:
break;
Expand All @@ -391,6 +398,7 @@ void menu_handler(void*, void* in_item_ref)
stop_and_clear_xpanel_plugin();
if (init_and_start_xpanel_plugin() != EXIT_SUCCESS)
Logger(TLogLevel::logERROR) << "Plugin reload error" << std::endl;
Logger(TLogLevel::logTRACE) << "Reload plugin done" << std::endl;
break;
default:
//
Expand Down

0 comments on commit 6ab6b44

Please sign in to comment.