diff options
Diffstat (limited to 'x-plane-plugin')
| -rw-r--r-- | x-plane-plugin/CMakeLists.txt | 48 | ||||
| -rw-r--r-- | x-plane-plugin/plugin.c | 112 |
2 files changed, 101 insertions, 59 deletions
diff --git a/x-plane-plugin/CMakeLists.txt b/x-plane-plugin/CMakeLists.txt index 32ec16e2..8e8fb338 100644 --- a/x-plane-plugin/CMakeLists.txt +++ b/x-plane-plugin/CMakeLists.txt @@ -1,36 +1,46 @@ if(LINUX OR APPLE) - set(SDK_XPLANE "" CACHE PATH "Path to X-Plane SDK") + set(SDK_XPLANE "" CACHE PATH "Path to the X-Plane SDK") if(SDK_XPLANE) - otr_module(xplane-plugin NO-QT) + if (APPLE) + otr_module(xplane-plugin NO-QT NO-INSTALL) + else() + otr_module(xplane-plugin NO-QT) + endif() # probably librt already included - install(FILES ${opentrack-xplane-plugin-c} DESTINATION "${opentrack-doc-src-pfx}/opentrack-xplane-plugin") - target_include_directories(opentrack-xplane-plugin SYSTEM PUBLIC ${SDK_XPLANE}/CHeaders ${SDK_XPLANE}/CHeaders/XPLM) + #install(FILES ${opentrack-xplane-plugin-c} DESTINATION "opentrack-libexec") + target_include_directories(opentrack-xplane-plugin SYSTEM PUBLIC ${SDK_XPLANE}/CHeaders/XPLM) if(APPLE) - set_property(TARGET opentrack-xplane-plugin APPEND_STRING PROPERTY - COMPILE_FLAGS "-iframework ${SDK_XPLANE}/Libraries/Mac/ -DAPL -DXPLM200 -DXPLM210 -framework XPLM -framework XPWidgets ") - set_property(TARGET opentrack-xplane-plugin APPEND_STRING PROPERTY - LINK_FLAGS "-F${SDK_XPLANE}/Libraries/Mac/ -framework XPLM -framework XPWidgets ") + target_compile_options(${self} PRIVATE + -iframework "${SDK_XPLANE}/Libraries/Mac/" + -DAPL -DXPLM200 -DXPLM210 + -framework XPLM) + target_link_options(${self} PRIVATE + "-F${SDK_XPLANE}/Libraries/Mac/" + -framework XPLM) + + install(TARGETS "${self}" + RUNTIME DESTINATION ${opentrack-bin}/xplane + BUNDLE DESTINATION ${opentrack-bin}/xplane + LIBRARY DESTINATION ${opentrack-bin}/xplane + PERMISSIONS ${opentrack-perms-exec}) + elseif(CMAKE_COMPILER_IS_GNUCXX) - set_property(TARGET opentrack-xplane-plugin APPEND_STRING PROPERTY - COMPILE_FLAGS "-fPIC -DLIN -DXPLM200 -DXPLM210 ") - set_property(TARGET opentrack-xplane-plugin APPEND_STRING PROPERTY - LINK_FLAGS "-rdynamic -nodefaultlibs -fPIC ") + target_compile_options(${self} PRIVATE -DLIN -DXPLM200 -DXPLM210) + target_link_options(${self} PRIVATE -rdynamic -nodefaultlibs) endif() - if(CMAKE_COMPILER_IS_GNUCXX AND NOT CMAKE_COMPILER_IS_CLANG) - set_property(TARGET opentrack-xplane-plugin APPEND_STRING PROPERTY - LINK_FLAGS "-undefined_warning ") + if(CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_COMPILER_IS_CLANG) + target_link_options(${self} PRIVATE -undefined_warning) endif() - set_target_properties(opentrack-xplane-plugin PROPERTIES + set_target_properties(${self} PROPERTIES LIBRARY_OUTPUT_NAME "opentrack.xpl" - PREFIX "" - SUFFIX "") + PREFIX "" SUFFIX "") if(UNIX AND NOT APPLE) - target_link_libraries(opentrack-xplane-plugin rt) + target_link_libraries(${self} rt) endif() endif() endif() diff --git a/x-plane-plugin/plugin.c b/x-plane-plugin/plugin.c index 1c41daf0..e43ee0ef 100644 --- a/x-plane-plugin/plugin.c +++ b/x-plane-plugin/plugin.c @@ -31,8 +31,17 @@ #define WINE_SHM_NAME "facetracknoir-wine-shm" #define WINE_MTX_NAME "facetracknoir-wine-mtx" -#define BUILD_compat -#include "compat/export.hpp" +#include "compat/linkage-macros.hpp" + +#ifndef MAP_FAILED +# define MAP_FAILED ((void*)-1) +#endif + +#ifdef __GNUC__ +# pragma GCC diagnostic ignored "-Wimplicit-float-conversion" +# pragma GCC diagnostic ignored "-Wdouble-promotion" +# pragma GCC diagnostic ignored "-Wlanguage-extension-token" +#endif enum Axis { TX = 0, TY, TZ, Yaw, Pitch, Roll @@ -50,7 +59,7 @@ typedef struct WineSHM int gameid, gameid2; unsigned char table[8]; bool stop; -} WineSHM; +} volatile WineSHM; static shm_wrapper* lck_posix = NULL; static WineSHM* shm_posix = NULL; @@ -100,11 +109,10 @@ void shm_wrapper_unlock(shm_wrapper* self) flock(self->fd, LOCK_UN); } -float write_head_position( - float inElapsedSinceLastCall, - float inElapsedTimeSinceLastFlightLoop, - int inCounter, - void * inRefcon ) +float write_head_position(float inElapsedSinceLastCall, + float inElapsedTimeSinceLastFlightLoop, + int inCounter, + void* inRefcon) { if (lck_posix != NULL && shm_posix != NULL) { shm_wrapper_lock(lck_posix); @@ -122,17 +130,19 @@ float write_head_position( return -1.0; } -static int TrackToggleHandler( XPLMCommandRef inCommand, - XPLMCommandPhase inPhase, - void * inRefCon ) +static int TrackToggleHandler(XPLMCommandRef inCommand, + XPLMCommandPhase inPhase, + void* inRefCon) { - if ( track_disabled ) + if (inPhase != xplm_CommandBegin) return 0; + + if (track_disabled) { //Enable - XPLMRegisterFlightLoopCallback(write_head_position, -1.0, NULL); + XPLMRegisterFlightLoopCallback(write_head_position, -1, NULL); // Reinit the offsets when we re-enable the plugin - if ( !translation_disabled ) + if (!translation_disabled) reinit_offset(); } else @@ -144,10 +154,12 @@ static int TrackToggleHandler( XPLMCommandRef inCommand, return 0; } -static int TranslationToggleHandler( XPLMCommandRef inCommand, - XPLMCommandPhase inPhase, - void * inRefCon ) +static int TranslationToggleHandler(XPLMCommandRef inCommand, + XPLMCommandPhase inPhase, + void* inRefCon) { + if (inPhase != xplm_CommandBegin) return 0; + translation_disabled = !translation_disabled; if (!translation_disabled) { @@ -157,36 +169,53 @@ static int TranslationToggleHandler( XPLMCommandRef inCommand, return 0; } -PLUGIN_API OTR_COMPAT_EXPORT 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"); +static inline +void volatile_explicit_bzero(void volatile* restrict ptr, size_t len) +{ + for (size_t i = 0; i < len; i++) + *((char volatile* restrict)ptr + i) = 0; + + asm volatile("" ::: "memory"); +} + +PLUGIN_API OTR_GENERIC_EXPORT +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"); + // view_roll = XPLMFindDataRef("sim/graphics/view/field_of_view_roll_deg"); + + view_x = XPLMFindDataRef("sim/graphics/view/pilots_head_x"); + view_y = XPLMFindDataRef("sim/graphics/view/pilots_head_y"); + view_z = XPLMFindDataRef("sim/graphics/view/pilots_head_z"); view_heading = XPLMFindDataRef("sim/graphics/view/pilots_head_psi"); view_pitch = XPLMFindDataRef("sim/graphics/view/pilots_head_the"); - view_roll = XPLMFindDataRef("sim/graphics/view/field_of_view_roll_deg"); + view_roll = XPLMFindDataRef("sim/graphics/view/pilots_head_phi"); 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(track_toggle, + TrackToggleHandler, + 1, + NULL); - XPLMRegisterCommandHandler( translation_disable_toggle, - TranslationToggleHandler, - 1, - (void*)0); + XPLMRegisterCommandHandler(translation_disable_toggle, + TranslationToggleHandler, + 1, + NULL); if (view_x && view_y && view_z && view_heading && view_pitch && track_toggle && translation_disable_toggle) { lck_posix = shm_wrapper_init(WINE_SHM_NAME, WINE_MTX_NAME, sizeof(WineSHM)); - if (lck_posix->mem == (void*)-1) { + if (lck_posix->mem == MAP_FAILED) { fprintf(stderr, "opentrack failed to init SHM!\n"); return 0; } - shm_posix = (WineSHM*) lck_posix->mem; - memset(shm_posix, 0, sizeof(WineSHM)); + shm_posix = lck_posix->mem; + volatile_explicit_bzero(shm_posix, sizeof(WineSHM)); strcpy(outName, "opentrack"); strcpy(outSignature, "opentrack - freetrack lives!"); strcpy(outDescription, "head tracking view control"); @@ -196,7 +225,8 @@ PLUGIN_API OTR_COMPAT_EXPORT int XPluginStart ( char * outName, char * outSignat return 0; } -PLUGIN_API OTR_COMPAT_EXPORT void XPluginStop ( void ) { +PLUGIN_API OTR_GENERIC_EXPORT +void XPluginStop (void) { if (lck_posix) { shm_wrapper_free(lck_posix); @@ -205,21 +235,23 @@ PLUGIN_API OTR_COMPAT_EXPORT void XPluginStop ( void ) { } } -PLUGIN_API OTR_COMPAT_EXPORT int XPluginEnable ( void ) { +PLUGIN_API OTR_GENERIC_EXPORT +int XPluginEnable (void) { XPLMRegisterFlightLoopCallback(write_head_position, -1.0, NULL); track_disabled = 0; return 1; } -PLUGIN_API OTR_COMPAT_EXPORT void XPluginDisable ( void ) { +PLUGIN_API OTR_GENERIC_EXPORT +void XPluginDisable (void) { XPLMUnregisterFlightLoopCallback(write_head_position, NULL); track_disabled = 1; } -PLUGIN_API OTR_COMPAT_EXPORT void XPluginReceiveMessage( - XPLMPluginID inFromWho, - int inMessage, - void * inParam) +PLUGIN_API OTR_GENERIC_EXPORT +void XPluginReceiveMessage(XPLMPluginID inFromWho, + int inMessage, + void * inParam) { if (inMessage == XPLM_MSG_AIRPORT_LOADED) reinit_offset(); |
