diff options
-rw-r--r-- | compat/shm.cpp | 69 | ||||
-rw-r--r-- | compat/shm.h | 8 | ||||
-rw-r--r-- | logic/runtime-libraries.cpp (renamed from logic/selected-libraries.cpp) | 0 | ||||
-rw-r--r-- | logic/runtime-libraries.hpp (renamed from logic/selected-libraries.hpp) | 2 | ||||
-rw-r--r-- | logic/tracker.h | 2 | ||||
-rw-r--r-- | logic/work.hpp | 2 | ||||
-rw-r--r-- | proto-ft/ftnoir_protocol_ft.h | 2 | ||||
-rw-r--r-- | proto-wine/ftnoir_protocol_wine.h | 2 | ||||
-rw-r--r-- | proto-wine/opentrack-wrapper-wine-posix.cxx | 2 | ||||
-rw-r--r-- | proto-wine/opentrack-wrapper-wine-windows.cxx | 2 | ||||
-rw-r--r-- | x-plane-plugin/plugin.c | 24 |
11 files changed, 63 insertions, 52 deletions
diff --git a/compat/shm.cpp b/compat/shm.cpp index a933f7f9..f09365f9 100644 --- a/compat/shm.cpp +++ b/compat/shm.cpp @@ -108,29 +108,34 @@ cleanup: } }; -PortableLockedShm::PortableLockedShm(const char* shmName, const char* mutexName, int mapSize) +shm_wrapper::shm_wrapper(const char* shm_name, const char* mutex_name, int map_size) { secattr sa(GENERIC_ALL|SYNCHRONIZE); - hMutex = CreateMutexA(sa.success ? &sa.attrs : nullptr, false, mutexName); - - if (!hMutex) + if (mutex_name == nullptr) + mutex = nullptr; + else { -#if !defined __WINE__ - qDebug() << "CreateMutexA:" << (int) GetLastError(); -#endif - return; + mutex = CreateMutexA(sa.success ? &sa.attrs : nullptr, false, mutex_name); + + if (!mutex) + { + #if !defined __WINE__ + qDebug() << "CreateMutexA:" << (int) GetLastError(); + #endif + return; + } } - hMapFile = CreateFileMappingA( + mapped_file = CreateFileMappingA( INVALID_HANDLE_VALUE, sa.success ? &sa.attrs : nullptr, PAGE_READWRITE, 0, - mapSize, - shmName); + map_size, + shm_name); - if (!hMapFile) + if (!mapped_file) { #if !defined __WINE__ qDebug() << "CreateFileMappingA:", (int) GetLastError(); @@ -139,11 +144,11 @@ PortableLockedShm::PortableLockedShm(const char* shmName, const char* mutexName, return; } - mem = MapViewOfFile(hMapFile, + mem = MapViewOfFile(mapped_file, FILE_MAP_WRITE, 0, 0, - mapSize); + map_size); if (!mem) { @@ -153,15 +158,15 @@ PortableLockedShm::PortableLockedShm(const char* shmName, const char* mutexName, } } -PortableLockedShm::~PortableLockedShm() +shm_wrapper::~shm_wrapper() { if(!UnmapViewOfFile(mem)) goto fail; - if (!CloseHandle(hMapFile)) + if (!CloseHandle(mapped_file)) goto fail; - if (!CloseHandle(hMutex)) + if (mutex && !CloseHandle(mutex)) goto fail; return; @@ -173,48 +178,54 @@ fail: #endif } -bool PortableLockedShm::lock() +bool shm_wrapper::lock() { - return WaitForSingleObject(hMutex, INFINITE) == WAIT_OBJECT_0; + if (mutex) + return WaitForSingleObject(mutex, INFINITE) == WAIT_OBJECT_0; + else + return false; } -bool PortableLockedShm::unlock() +bool shm_wrapper::unlock() { - return ReleaseMutex(hMutex); + if (mutex) + return ReleaseMutex(mutex); + else + return false; } #else #include <limits.h> #pragma GCC diagnostic ignored "-Wunused-result" -PortableLockedShm::PortableLockedShm(const char *shmName, const char* /*mutexName*/, int mapSize) : size(mapSize) +shm_wrapper::shm_wrapper(const char *shm_name, const char* /*mutex_name*/, int map_size) : size(mapSize) { char filename[PATH_MAX+2] {}; strcpy(filename, "/"); - strcat(filename, shmName); + strcat(filename, shm_name); fd = shm_open(filename, O_RDWR | O_CREAT, 0600); - (void) ftruncate(fd, mapSize); - mem = mmap(NULL, mapSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0); + (void) ftruncate(fd, map_size); + mem = mmap(NULL, map_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0); } -PortableLockedShm::~PortableLockedShm() +shm_wrapper::~shm_wrapper() { (void) munmap(mem, size); (void) close(fd); } -bool PortableLockedShm::lock() +bool shm_wrapper::lock() { return flock(fd, LOCK_EX) == 0; } -bool PortableLockedShm::unlock() +bool shm_wrapper::unlock() { return flock(fd, LOCK_UN) == 0; } #endif -bool PortableLockedShm::success() +bool shm_wrapper::success() { #ifndef _WIN32 return mem != (void*) -1; diff --git a/compat/shm.h b/compat/shm.h index 3b37a8a3..61efaf68 100644 --- a/compat/shm.h +++ b/compat/shm.h @@ -21,18 +21,18 @@ #include "export.hpp" -class OTR_COMPAT_EXPORT PortableLockedShm final +class OTR_COMPAT_EXPORT shm_wrapper final { void* mem; #if defined(_WIN32) - HANDLE hMutex, hMapFile; + HANDLE mutex, mapped_file; #else int fd, size; #endif public: - PortableLockedShm(const char *shmName, const char *mutexName, int mapSize); - ~PortableLockedShm(); + shm_wrapper(const char *shm_name, const char *mutex_name, int map_size); + ~shm_wrapper(); bool lock(); bool unlock(); bool success(); diff --git a/logic/selected-libraries.cpp b/logic/runtime-libraries.cpp index fbe30fef..fbe30fef 100644 --- a/logic/selected-libraries.cpp +++ b/logic/runtime-libraries.cpp diff --git a/logic/selected-libraries.hpp b/logic/runtime-libraries.hpp index 586f7a57..6cfd8b57 100644 --- a/logic/selected-libraries.hpp +++ b/logic/runtime-libraries.hpp @@ -29,7 +29,7 @@ struct runtime_event_handler void run_events(ext_event_ordinal k, Pose& pose); }; -struct OTR_LOGIC_EXPORT runtime_libraries : runtime_event_handler +struct OTR_LOGIC_EXPORT runtime_libraries final : runtime_event_handler { using dylibptr = std::shared_ptr<dylib>; diff --git a/logic/tracker.h b/logic/tracker.h index 1151d16b..afd57112 100644 --- a/logic/tracker.h +++ b/logic/tracker.h @@ -14,7 +14,7 @@ #include "api/plugin-support.hpp" #include "mappings.hpp" #include "compat/euler.hpp" -#include "selected-libraries.hpp" +#include "runtime-libraries.hpp" #include "spline/spline.hpp" #include "main-settings.hpp" diff --git a/logic/work.hpp b/logic/work.hpp index fa0e5c90..1aff5260 100644 --- a/logic/work.hpp +++ b/logic/work.hpp @@ -14,7 +14,7 @@ #include "shortcuts.h" #include "export.hpp" #include "tracklogger.hpp" -#include "logic/selected-libraries.hpp" +#include "logic/runtime-libraries.hpp" #include "api/plugin-support.hpp" #include <QObject> diff --git a/proto-ft/ftnoir_protocol_ft.h b/proto-ft/ftnoir_protocol_ft.h index 5b8e848f..43c1c3fc 100644 --- a/proto-ft/ftnoir_protocol_ft.h +++ b/proto-ft/ftnoir_protocol_ft.h @@ -57,7 +57,7 @@ public: } private: settings s; - PortableLockedShm shm; + shm_wrapper shm; FTHeap volatile *pMemData; QLibrary FTIRViewsLib; diff --git a/proto-wine/ftnoir_protocol_wine.h b/proto-wine/ftnoir_protocol_wine.h index a0a3fa4c..e996a2c2 100644 --- a/proto-wine/ftnoir_protocol_wine.h +++ b/proto-wine/ftnoir_protocol_wine.h @@ -25,7 +25,7 @@ public: return connected_game; } private: - PortableLockedShm lck_shm; + shm_wrapper lck_shm; WineSHM* shm; QProcess wrapper; int gameid; diff --git a/proto-wine/opentrack-wrapper-wine-posix.cxx b/proto-wine/opentrack-wrapper-wine-posix.cxx index 21c7622b..e36407a9 100644 --- a/proto-wine/opentrack-wrapper-wine-posix.cxx +++ b/proto-wine/opentrack-wrapper-wine-posix.cxx @@ -2,6 +2,6 @@ # undef _WIN32 #endif -#define PortableLockedShm ShmPosix +#define shm_wrapper ShmPosix #include "compat/shm.h" #include "compat/shm.cpp" diff --git a/proto-wine/opentrack-wrapper-wine-windows.cxx b/proto-wine/opentrack-wrapper-wine-windows.cxx index 698e3c3b..3c315d84 100644 --- a/proto-wine/opentrack-wrapper-wine-windows.cxx +++ b/proto-wine/opentrack-wrapper-wine-windows.cxx @@ -2,7 +2,7 @@ # error "bad cross" #endif -#define PortableLockedShm ShmWine +#define shm_wrapper ShmWine #include "compat/shm.h" #include "compat/shm.cpp" #include "wine-shm.h" diff --git a/x-plane-plugin/plugin.c b/x-plane-plugin/plugin.c index abf34b5f..26ea18e9 100644 --- a/x-plane-plugin/plugin.c +++ b/x-plane-plugin/plugin.c @@ -38,11 +38,11 @@ enum Axis { TX = 0, TY, TZ, Yaw, Pitch, Roll }; -typedef struct PortableLockedShm +typedef struct shm_wrapper { void* mem; int fd, size; -} PortableLockedShm; +} shm_wrapper; typedef struct WineSHM { @@ -52,7 +52,7 @@ typedef struct WineSHM bool stop; } WineSHM; -static PortableLockedShm* lck_posix = NULL; +static shm_wrapper* lck_posix = NULL; static WineSHM* shm_posix = NULL; static WineSHM* data_last = NULL; static void *view_x, *view_y, *view_z, *view_heading, *view_pitch, *view_roll; @@ -73,9 +73,9 @@ static void reinit_offset() { # define unused(varname) varname #endif -PortableLockedShm* PortableLockedShm_init(const char *shmName, const char *unused(mutexName), int mapSize) +shm_wrapper* shm_wrapper_init(const char *shmName, const char *unused(mutexName), int mapSize) { - PortableLockedShm* self = malloc(sizeof(PortableLockedShm)); + shm_wrapper* self = malloc(sizeof(shm_wrapper)); char shm_filename[NAME_MAX]; shm_filename[0] = '/'; strncpy(shm_filename+1, shmName, NAME_MAX-2); @@ -88,7 +88,7 @@ PortableLockedShm* PortableLockedShm_init(const char *shmName, const char *unuse return self; } -void PortableLockedShm_free(PortableLockedShm* self) +void shm_wrapper_free(shm_wrapper* self) { /*(void) shm_unlink(shm_filename);*/ (void) munmap(self->mem, self->size); @@ -96,12 +96,12 @@ void PortableLockedShm_free(PortableLockedShm* self) free(self); } -void PortableLockedShm_lock(PortableLockedShm* self) +void shm_wrapper_lock(shm_wrapper* self) { flock(self->fd, LOCK_SH); } -void PortableLockedShm_unlock(PortableLockedShm* self) +void shm_wrapper_unlock(shm_wrapper* self) { flock(self->fd, LOCK_UN); } @@ -119,7 +119,7 @@ float write_head_position( //only set the view if tracking is running if(memcmp(shm_posix, data_last, sizeof(shm_posix->data)) != 0){ - PortableLockedShm_lock(lck_posix); + shm_wrapper_lock(lck_posix); if (!translation_disabled) { XPLMSetDataf(view_x, shm_posix->data[TX] * 1e-3 + offset_x); @@ -136,7 +136,7 @@ float write_head_position( memcpy(&data_last, &shm_posix, sizeof(WineSHM)); - PortableLockedShm_unlock(lck_posix); + shm_wrapper_unlock(lck_posix); } return -1.0; } @@ -199,7 +199,7 @@ PLUGIN_API OTR_COMPAT_EXPORT int XPluginStart ( char * outName, char * outSignat 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)); + lck_posix = shm_wrapper_init(WINE_SHM_NAME, WINE_MTX_NAME, sizeof(WineSHM)); if (lck_posix->mem == (void*)-1) { fprintf(stderr, "opentrack failed to init SHM!\n"); return 0; @@ -218,7 +218,7 @@ PLUGIN_API OTR_COMPAT_EXPORT int XPluginStart ( char * outName, char * outSignat PLUGIN_API OTR_COMPAT_EXPORT void XPluginStop ( void ) { if (lck_posix) { - PortableLockedShm_free(lck_posix); + shm_wrapper_free(lck_posix); lck_posix = NULL; shm_posix = NULL; } |