From 59144a96bf9e3391a32b9ad8f5240f02acb3f44a Mon Sep 17 00:00:00 2001 From: Ennio Barbaro Date: Thu, 2 Jul 2015 22:08:40 +0200 Subject: Use XPLMRegisterFlightLoopCallback instead of XPLMRegisterDrawCallback --- CMakeLists.txt | 4 ++-- x-plane-plugin/plugin.c | 17 ++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ec6dc54..4fb6e8d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -199,13 +199,13 @@ if(SDK_XPLANE) SET_TARGET_PROPERTIES(opentrack-xplane-plugin PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_SOURCE_DIR}/x-plane-plugin/version-script.txt -shared -rdynamic -nodefaultlibs -undefined_warning -fPIC" - COMPILE_FLAGS "-Wall -O2 -pipe -fPIC -DLIN -DXPLM210" + COMPILE_FLAGS "-Wall -O2 -pipe -fPIC -DLIN -DXPLM200 -DXPLM210" LIBRARY_OUTPUT_NAME "opentrack.xpl" PREFIX "" SUFFIX "") endif() if(APPLE) SET_TARGET_PROPERTIES(opentrack-xplane-plugin PROPERTIES - COMPILE_FLAGS "-iframework ${SDK_XPLANE}/Libraries/Mac/ -DAPL -DXPLM210 -framework XPLM -framework XPWidgets" + COMPILE_FLAGS "-iframework ${SDK_XPLANE}/Libraries/Mac/ -DAPL -DXPLM200 -DXPLM210 -framework XPLM -framework XPWidgets" LINK_FLAGS "-F${SDK_XPLANE}/Libraries/Mac/ -framework XPLM -framework XPWidgets") endif() if(UNIX AND NOT APPLE) diff --git a/x-plane-plugin/plugin.c b/x-plane-plugin/plugin.c index 3958c895..eed3c774 100644 --- a/x-plane-plugin/plugin.c +++ b/x-plane-plugin/plugin.c @@ -8,9 +8,7 @@ #include #include -#include #include -#include #include #ifndef PLUGIN_API @@ -87,10 +85,11 @@ void PortableLockedShm_unlock(PortableLockedShm* self) flock(self->fd, LOCK_UN); } -int write_head_position( - XPLMDrawingPhase OT_UNUSED(inPhase), - int OT_UNUSED(inIsBefore), - void * OT_UNUSED(inRefcon)) +float write_head_position( + float OT_UNUSED(inElapsedSinceLastCall), + float OT_UNUSED(inElapsedTimeSinceLastFlightLoop), + int OT_UNUSED(inCounter), + void * OT_UNUSED(inRefcon) ) { if (lck_posix != NULL && shm_posix != NULL) { PortableLockedShm_lock(lck_posix); @@ -101,7 +100,7 @@ int write_head_position( XPLMSetDataf(view_pitch, shm_posix->data[Pitch] * 180 / 3.141592654); PortableLockedShm_unlock(lck_posix); } - return 1; + return -1.0; } PLUGIN_API int XPluginStart ( char * outName, char * outSignature, char * outDescription ) { @@ -137,11 +136,11 @@ PLUGIN_API void XPluginStop ( void ) { } PLUGIN_API void XPluginEnable ( void ) { - XPLMRegisterDrawCallback(write_head_position, xplm_Phase_LastScene, 1, NULL); + XPLMRegisterFlightLoopCallback(write_head_position, -1.0, NULL); } PLUGIN_API void XPluginDisable ( void ) { - XPLMUnregisterDrawCallback(write_head_position, xplm_Phase_LastScene, 1, NULL); + XPLMUnregisterFlightLoopCallback(write_head_position, NULL); } PLUGIN_API void XPluginReceiveMessage( -- cgit v1.2.3 From 1e1e4d6bf51805c30feee39083a1ee79f09f8641 Mon Sep 17 00:00:00 2001 From: Ennio Barbaro Date: Thu, 2 Jul 2015 22:12:55 +0200 Subject: Added enable/disable plugin command. --- x-plane-plugin/plugin.c | 58 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/x-plane-plugin/plugin.c b/x-plane-plugin/plugin.c index eed3c774..1643fc9c 100644 --- a/x-plane-plugin/plugin.c +++ b/x-plane-plugin/plugin.c @@ -10,6 +10,7 @@ #include #include #include +#include #ifndef PLUGIN_API #define PLUGIN_API @@ -39,6 +40,9 @@ static PortableLockedShm* lck_posix = NULL; static WineSHM* shm_posix = NULL; static void *view_x, *view_y, *view_z, *view_heading, *view_pitch; static float offset_x, offset_y, offset_z; +static XPLMCommandRef track_toggle = NULL, translation_disable_toggle = NULL; +static int track_disabled = 1; +static int translation_disabled; static void reinit_offset() { offset_x = XPLMGetDataf(view_x); @@ -93,9 +97,12 @@ float write_head_position( { if (lck_posix != NULL && shm_posix != NULL) { PortableLockedShm_lock(lck_posix); - XPLMSetDataf(view_x, shm_posix->data[TX] * 1e-3 + offset_x); - XPLMSetDataf(view_y, shm_posix->data[TY] * 1e-3 + offset_y); - XPLMSetDataf(view_z, shm_posix->data[TZ] * 1e-3 + offset_z); + if (!translation_disabled) + { + XPLMSetDataf(view_x, shm_posix->data[TX] * 1e-3 + offset_x); + XPLMSetDataf(view_y, shm_posix->data[TY] * 1e-3 + offset_y); + XPLMSetDataf(view_z, shm_posix->data[TZ] * 1e-3 + offset_z); + } XPLMSetDataf(view_heading, shm_posix->data[Yaw] * 180 / 3.141592654); XPLMSetDataf(view_pitch, shm_posix->data[Pitch] * 180 / 3.141592654); PortableLockedShm_unlock(lck_posix); @@ -103,13 +110,54 @@ float write_head_position( return -1.0; } +static int TrackToggleHandler( XPLMCommandRef inCommand, + XPLMCommandPhase inPhase, + void * inRefCon ) +{ + if ( track_disabled ) + { + //Enable + XPLMRegisterFlightLoopCallback(write_head_position, -1.0, NULL); + } + else + { + //Disable + XPLMUnregisterFlightLoopCallback(write_head_position, NULL); + } + track_disabled = !track_disabled; + return 0; +} + +static int TranslationToggleHandler( XPLMCommandRef inCommand, + XPLMCommandPhase inPhase, + void * inRefCon ) +{ + translation_disabled = !translation_disabled; + return 0; +} + PLUGIN_API int XPluginStart ( char * outName, char * outSignature, char * outDescription ) { view_x = XPLMFindDataRef("sim/aircraft/view/acf_peX"); view_y = XPLMFindDataRef("sim/aircraft/view/acf_peY"); view_z = XPLMFindDataRef("sim/aircraft/view/acf_peZ"); view_heading = XPLMFindDataRef("sim/graphics/view/pilots_head_psi"); view_pitch = XPLMFindDataRef("sim/graphics/view/pilots_head_the"); - if (view_x && view_y && view_z && view_heading && view_pitch) { + + track_toggle = XPLMCreateCommand("opentrack/toggle", "Disable/Enable head tracking"); + translation_disable_toggle = XPLMCreateCommand("opentrack/toggle_translation", "Disable/Enable input translation from opentrack"); + + XPLMRegisterCommandHandler( track_toggle, + TrackToggleHandler, + 1, + (void*)0); + + XPLMRegisterCommandHandler( translation_disable_toggle, + TranslationToggleHandler, + 1, + (void*)0); + + + if (view_x && view_y && view_z && view_heading && view_pitch && track_toggle && translation_disable_toggle) { lck_posix = PortableLockedShm_init(WINE_SHM_NAME, WINE_MTX_NAME, sizeof(WineSHM)); if (lck_posix->mem == (void*)-1) { fprintf(stderr, "opentrack failed to init SHM!\n"); @@ -137,10 +185,12 @@ PLUGIN_API void XPluginStop ( void ) { PLUGIN_API void XPluginEnable ( void ) { XPLMRegisterFlightLoopCallback(write_head_position, -1.0, NULL); + track_disabled = 0; } PLUGIN_API void XPluginDisable ( void ) { XPLMUnregisterFlightLoopCallback(write_head_position, NULL); + track_disabled = 1; } PLUGIN_API void XPluginReceiveMessage( -- cgit v1.2.3 From 67d5b4a3fe4ea89aed3a7a674958c043eb25079d Mon Sep 17 00:00:00 2001 From: Ennio Barbaro Date: Thu, 2 Jul 2015 23:14:45 +0200 Subject: Reinit the offsets when re-enabling the plugin --- x-plane-plugin/plugin.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/x-plane-plugin/plugin.c b/x-plane-plugin/plugin.c index 1643fc9c..999f6e15 100644 --- a/x-plane-plugin/plugin.c +++ b/x-plane-plugin/plugin.c @@ -118,6 +118,10 @@ static int TrackToggleHandler( XPLMCommandRef inCommand, { //Enable XPLMRegisterFlightLoopCallback(write_head_position, -1.0, NULL); + + // Reinit the offsets when we re-enable the plugin + if ( !translation_disabled ) + reinit_offset(); } else { @@ -133,6 +137,11 @@ static int TranslationToggleHandler( XPLMCommandRef inCommand, void * inRefCon ) { translation_disabled = !translation_disabled; + if (!translation_disabled) + { + // Reinit the offsets when we re-enable the translations so that we can "move around" + reinit_offset(); + } return 0; } -- cgit v1.2.3 From 44d0136cb4b532c957d6c037ea6a862a1e371cd9 Mon Sep 17 00:00:00 2001 From: Ennio Barbaro Date: Thu, 2 Jul 2015 23:24:52 +0200 Subject: Fixed bug crash if dlopen fails --- opentrack/plugin-support.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/opentrack/plugin-support.hpp b/opentrack/plugin-support.hpp index bc07c106..95b4b0a1 100644 --- a/opentrack/plugin-support.hpp +++ b/opentrack/plugin-support.hpp @@ -115,7 +115,8 @@ struct dylib { { fprintf(stderr, "Error, ignoring: %s\n", err); fflush(stderr); - dlclose(handle); + if (handle) + dlclose(handle); handle = nullptr; return true; } @@ -138,6 +139,7 @@ struct dylib { return; } else { (void) _foo::err(handle); + return; } #endif -- cgit v1.2.3