From 819e635af2b64213f1076eb4a99bba8c48cfdb68 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 30 Sep 2017 11:28:32 +0200 Subject: remove camel case --- compat/shm.cpp | 69 ++++++++++++++---------- compat/shm.h | 8 +-- logic/runtime-libraries.cpp | 76 +++++++++++++++++++++++++++ logic/runtime-libraries.hpp | 44 ++++++++++++++++ logic/selected-libraries.cpp | 76 --------------------------- logic/selected-libraries.hpp | 44 ---------------- logic/tracker.h | 2 +- logic/work.hpp | 2 +- proto-ft/ftnoir_protocol_ft.h | 2 +- proto-wine/ftnoir_protocol_wine.h | 2 +- proto-wine/opentrack-wrapper-wine-posix.cxx | 2 +- proto-wine/opentrack-wrapper-wine-windows.cxx | 2 +- x-plane-plugin/plugin.c | 24 ++++----- 13 files changed, 182 insertions(+), 171 deletions(-) create mode 100644 logic/runtime-libraries.cpp create mode 100644 logic/runtime-libraries.hpp delete mode 100644 logic/selected-libraries.cpp delete mode 100644 logic/selected-libraries.hpp 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 #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/runtime-libraries.cpp b/logic/runtime-libraries.cpp new file mode 100644 index 00000000..fbe30fef --- /dev/null +++ b/logic/runtime-libraries.cpp @@ -0,0 +1,76 @@ +#include "selected-libraries.hpp" +#include "options/scoped.hpp" +#include + +using ext_ord = IExtension::event_ordinal; +using ext_mask = IExtension::event_mask; +using ext_fun_type = void(IExtension::*)(Pose&); + +static constexpr struct event_type_mapping +{ + ext_fun_type ptr; + ext_mask m; + ext_ord idx; +} ordinal_to_function[] = { + { &IExtension::process_raw, ext_mask::on_raw, ext_ord::ev_raw, }, + { &IExtension::process_after_center, ext_mask::on_after_center, ext_ord::ev_after_center, }, + { &IExtension::process_before_filter, ext_mask::on_before_filter, ext_ord::ev_before_filter, }, + { &IExtension::process_before_transform, ext_mask::on_before_transform, ext_ord::ev_before_transform, }, + { &IExtension::process_before_mapping, ext_mask::on_before_mapping, ext_ord::ev_before_mapping, }, + { &IExtension::process_finished, ext_mask::on_finished, ext_ord::ev_finished, }, +}; + +runtime_libraries::runtime_libraries(QFrame* frame, dylibptr t, dylibptr p, dylibptr f) : + pTracker(nullptr), + pFilter(nullptr), + pProtocol(nullptr), + correct(false) +{ + using namespace options; + + const bool prev_teardown_flag = opts::is_tracker_teardown(); + + opts::set_teardown_flag(true); + + pProtocol = make_dylib_instance(p); + + if (!pProtocol) + { + qDebug() << "protocol dylib load failure"; + goto end; + } + + if(!pProtocol->correct()) + { + qDebug() << "protocol load failure"; + pProtocol = nullptr; + goto end; + } + + pTracker = make_dylib_instance(t); + pFilter = make_dylib_instance(f); + + if (!pTracker) + { + qDebug() << "tracker dylib load failure"; + goto end; + } + + pTracker->start_tracker(frame); + + correct = true; +end: + opts::set_teardown_flag(prev_teardown_flag); +} + +void runtime_event_handler::run_events(ext_event_ordinal k, Pose& pose) +{ + auto fun = std::mem_fn(ordinal_to_function[k].ptr); + + for (ext& x : extension_events[k]) + { + if (x == nullptr) + break; + fun(x, pose); + } +} diff --git a/logic/runtime-libraries.hpp b/logic/runtime-libraries.hpp new file mode 100644 index 00000000..6cfd8b57 --- /dev/null +++ b/logic/runtime-libraries.hpp @@ -0,0 +1,44 @@ +/* Copyright (c) 2014-2015, Stanislaw Halik + + * Permission to use, copy, modify, and/or distribute this + * software for any purpose with or without fee is hereby granted, + * provided that the above copyright notice and this permission + * notice appear in all copies. + */ + +#pragma once + +#include "api/plugin-support.hpp" +#include "export.hpp" + +#include +#include + +#include + +struct runtime_event_handler +{ + using ext_event_ordinal = IExtension::event_ordinal; + using ext = std::shared_ptr; + + enum : unsigned { ext_max_events = 64 }; + using ext_list = std::array; + + std::array extension_events; + + void run_events(ext_event_ordinal k, Pose& pose); +}; + +struct OTR_LOGIC_EXPORT runtime_libraries final : runtime_event_handler +{ + using dylibptr = std::shared_ptr; + + std::shared_ptr pTracker; + std::shared_ptr pFilter; + std::shared_ptr pProtocol; + + runtime_libraries(QFrame* frame, dylibptr t, dylibptr p, dylibptr f); + runtime_libraries() : pTracker(nullptr), pFilter(nullptr), pProtocol(nullptr), correct(false) {} + + bool correct; +}; diff --git a/logic/selected-libraries.cpp b/logic/selected-libraries.cpp deleted file mode 100644 index fbe30fef..00000000 --- a/logic/selected-libraries.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include "selected-libraries.hpp" -#include "options/scoped.hpp" -#include - -using ext_ord = IExtension::event_ordinal; -using ext_mask = IExtension::event_mask; -using ext_fun_type = void(IExtension::*)(Pose&); - -static constexpr struct event_type_mapping -{ - ext_fun_type ptr; - ext_mask m; - ext_ord idx; -} ordinal_to_function[] = { - { &IExtension::process_raw, ext_mask::on_raw, ext_ord::ev_raw, }, - { &IExtension::process_after_center, ext_mask::on_after_center, ext_ord::ev_after_center, }, - { &IExtension::process_before_filter, ext_mask::on_before_filter, ext_ord::ev_before_filter, }, - { &IExtension::process_before_transform, ext_mask::on_before_transform, ext_ord::ev_before_transform, }, - { &IExtension::process_before_mapping, ext_mask::on_before_mapping, ext_ord::ev_before_mapping, }, - { &IExtension::process_finished, ext_mask::on_finished, ext_ord::ev_finished, }, -}; - -runtime_libraries::runtime_libraries(QFrame* frame, dylibptr t, dylibptr p, dylibptr f) : - pTracker(nullptr), - pFilter(nullptr), - pProtocol(nullptr), - correct(false) -{ - using namespace options; - - const bool prev_teardown_flag = opts::is_tracker_teardown(); - - opts::set_teardown_flag(true); - - pProtocol = make_dylib_instance(p); - - if (!pProtocol) - { - qDebug() << "protocol dylib load failure"; - goto end; - } - - if(!pProtocol->correct()) - { - qDebug() << "protocol load failure"; - pProtocol = nullptr; - goto end; - } - - pTracker = make_dylib_instance(t); - pFilter = make_dylib_instance(f); - - if (!pTracker) - { - qDebug() << "tracker dylib load failure"; - goto end; - } - - pTracker->start_tracker(frame); - - correct = true; -end: - opts::set_teardown_flag(prev_teardown_flag); -} - -void runtime_event_handler::run_events(ext_event_ordinal k, Pose& pose) -{ - auto fun = std::mem_fn(ordinal_to_function[k].ptr); - - for (ext& x : extension_events[k]) - { - if (x == nullptr) - break; - fun(x, pose); - } -} diff --git a/logic/selected-libraries.hpp b/logic/selected-libraries.hpp deleted file mode 100644 index 586f7a57..00000000 --- a/logic/selected-libraries.hpp +++ /dev/null @@ -1,44 +0,0 @@ -/* Copyright (c) 2014-2015, Stanislaw Halik - - * Permission to use, copy, modify, and/or distribute this - * software for any purpose with or without fee is hereby granted, - * provided that the above copyright notice and this permission - * notice appear in all copies. - */ - -#pragma once - -#include "api/plugin-support.hpp" -#include "export.hpp" - -#include -#include - -#include - -struct runtime_event_handler -{ - using ext_event_ordinal = IExtension::event_ordinal; - using ext = std::shared_ptr; - - enum : unsigned { ext_max_events = 64 }; - using ext_list = std::array; - - std::array extension_events; - - void run_events(ext_event_ordinal k, Pose& pose); -}; - -struct OTR_LOGIC_EXPORT runtime_libraries : runtime_event_handler -{ - using dylibptr = std::shared_ptr; - - std::shared_ptr pTracker; - std::shared_ptr pFilter; - std::shared_ptr pProtocol; - - runtime_libraries(QFrame* frame, dylibptr t, dylibptr p, dylibptr f); - runtime_libraries() : pTracker(nullptr), pFilter(nullptr), pProtocol(nullptr), correct(false) {} - - bool correct; -}; 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 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; } -- cgit v1.2.3