From 0497f57268e8be7af69d37e24fa356e441c0d5f6 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 1 Nov 2015 09:09:25 +0100 Subject: cmake: less linkage boilerplate --- opentrack-compat/shm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'opentrack-compat') diff --git a/opentrack-compat/shm.h b/opentrack-compat/shm.h index c74534e7..28bda986 100644 --- a/opentrack-compat/shm.h +++ b/opentrack-compat/shm.h @@ -28,7 +28,7 @@ # define COMPAT_GNUC_VISIBILITY #endif -#ifdef BUILD_opentrack_compat +#ifdef BUILD_compat # ifdef _WIN32 # define COMPAT_WIN32_EXPORT __declspec(dllexport) # else -- cgit v1.2.3 From 5a2ddaf39cf5ad86cf43d8a01f51989b5e3afa49 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 1 Nov 2015 09:20:17 +0100 Subject: cmake: export only needed symbols --- cmake/opentrack-macros.cmake | 22 +++++++++++++++------- csv/CMakeLists.txt | 2 +- opentrack-compat/CMakeLists.txt | 2 +- pose-widget/CMakeLists.txt | 2 +- qxt-mini/CMakeLists.txt | 2 +- spline-widget/CMakeLists.txt | 2 +- 6 files changed, 20 insertions(+), 12 deletions(-) (limited to 'opentrack-compat') diff --git a/cmake/opentrack-macros.cmake b/cmake/opentrack-macros.cmake index 46cf3c61..9be09397 100644 --- a/cmake/opentrack-macros.cmake +++ b/cmake/opentrack-macros.cmake @@ -32,7 +32,7 @@ endfunction() macro(opentrack_library n dir) cmake_parse_arguments(opentrack-foolib - "NO-LIBRARY;STATIC;NO-COMPAT;NO-LINKER-SCRIPT" + "NO-LIBRARY;STATIC;NO-COMPAT;NO-LINKER-SCRIPT;LINKAGE" "LINK;COMPILE;GNU-LINK;GNU-COMPILE" "" ${ARGN} @@ -54,17 +54,25 @@ macro(opentrack_library n dir) endif() target_link_libraries(${n} ${MY_QT_LIBS}) if(CMAKE_COMPILER_IS_GNUCXX AND NOT APPLE AND NOT opentrack-foolib_NO-LINKER-SCRIPT) - SET_TARGET_PROPERTIES(${n} PROPERTIES + set_target_properties(${n} PROPERTIES LINK_FLAGS "${opentrack-foolib_LINK} ${opentrack-foolib_GNU-LINK} -Wl,--as-needed -Wl,--version-script=\"${CMAKE_SOURCE_DIR}/opentrack-compat/${version-script}-version-script.txt\"" COMPILE_FLAGS "${opentrack-foolib_COMPILE} ${opentrack-foolib_GNU-COMPILE} -fvisibility=hidden -fvisibility-inlines-hidden" ) else() - set(link-flags) - if(MSVC) - set(link-flags "${msvc-subsystem} /DEBUG /OPT:ICF") + set(c-props) + set(l-props) + if(opentrack-foolib_LINKAGE AND CMAKE_COMPILER_IS_GNUCXX AND NOT APPLE) + set(c-props "${opentrack-foolib_COMPILE} ${opentrack-foolib_GNU-COMPILE} -fvisibility=hidden -fvisibility-inlines-hidden") + set(l-props "${opentrack-foolib_LINK} ${opentrack-foolib_GNU-LINK} -Wl,--as-needed") + else() + if(MSVC) + set(l-props "${msvc-subsystem} /DEBUG /OPT:ICF") + endif() endif() - set_target_properties(${n} PROPERTIES LINK_FLAGS "${link-flags} ${opentrack-foolib_LINK}" COMPILE_FLAGS "${opentrack-foolib_COMPILE}") - set(link-flags) + set_target_properties(${n} PROPERTIES + LINK_FLAGS "${l-props} ${opentrack-foolib_LINK}" + COMPILE_FLAGS "${c-props} ${opentrack-foolib_COMPILE}" + ) endif() string(REGEX REPLACE "^opentrack-" "" n_ ${n}) string(REPLACE "-" "_" n_ ${n_}) diff --git a/csv/CMakeLists.txt b/csv/CMakeLists.txt index 7e80317c..7bafc7ff 100644 --- a/csv/CMakeLists.txt +++ b/csv/CMakeLists.txt @@ -1 +1 @@ -opentrack_boilerplate(opentrack-csv NO-LINKER-SCRIPT) +opentrack_boilerplate(opentrack-csv NO-LINKER-SCRIPT LINKAGE) diff --git a/opentrack-compat/CMakeLists.txt b/opentrack-compat/CMakeLists.txt index a92f4f31..37d53e6c 100644 --- a/opentrack-compat/CMakeLists.txt +++ b/opentrack-compat/CMakeLists.txt @@ -1,4 +1,4 @@ -opentrack_boilerplate(opentrack-compat NO-COMPAT NO-LINKER-SCRIPT) +opentrack_boilerplate(opentrack-compat NO-COMPAT NO-LINKER-SCRIPT LINKAGE) if(NOT WIN32 AND NOT APPLE) target_link_libraries(opentrack-compat rt) endif() diff --git a/pose-widget/CMakeLists.txt b/pose-widget/CMakeLists.txt index 66cafef7..e48660d7 100644 --- a/pose-widget/CMakeLists.txt +++ b/pose-widget/CMakeLists.txt @@ -1 +1 @@ -opentrack_boilerplate(opentrack-pose-widget NO-LINKER-SCRIPT) +opentrack_boilerplate(opentrack-pose-widget NO-LINKER-SCRIPT LINKAGE) diff --git a/qxt-mini/CMakeLists.txt b/qxt-mini/CMakeLists.txt index 5de29cd1..a8b8fb1d 100644 --- a/qxt-mini/CMakeLists.txt +++ b/qxt-mini/CMakeLists.txt @@ -1,5 +1,5 @@ if(UNIX OR APPLE) - opentrack_boilerplate(opentrack-qxt-mini NO-LINKER-SCRIPT NO-COMPAT) + opentrack_boilerplate(opentrack-qxt-mini NO-LINKER-SCRIPT NO-COMPAT LINKAGE) if(NOT APPLE) target_link_libraries(opentrack-qxt-mini X11) else() diff --git a/spline-widget/CMakeLists.txt b/spline-widget/CMakeLists.txt index 93958f50..4d951f92 100644 --- a/spline-widget/CMakeLists.txt +++ b/spline-widget/CMakeLists.txt @@ -1 +1 @@ -opentrack_boilerplate(opentrack-spline-widget NO-LINKER-SCRIPT NO-COMPAT) +opentrack_boilerplate(opentrack-spline-widget NO-LINKER-SCRIPT NO-COMPAT LINKAGE) -- cgit v1.2.3 From 0424a2802aec3600774ba9347c49312bb09b8cfe Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 1 Nov 2015 11:28:17 +0100 Subject: all: adjust options.hpp move to compat --- filter-accela/ftnoir_filter_accela.h | 2 +- filter-ewma2/ftnoir_filter_ewma2.h | 2 +- filter-kalman/ftnoir_filter_kalman.h | 2 +- gui/main.cpp | 2 +- gui/new_file_dialog.h | 2 +- gui/process_detector.h | 2 +- gui/ui.cpp | 4 +-- gui/ui.h | 2 +- opentrack-compat/compat-export.hpp | 15 +++++++++ opentrack-compat/compat-import.hpp | 15 +++++++++ opentrack-compat/export.hpp | 22 +++++++------ opentrack-compat/import.hpp | 15 +++++++++ opentrack-compat/shm.h | 22 ++----------- opentrack/CMakeLists.txt | 2 +- opentrack/main-settings.hpp | 2 +- opentrack/mappings.hpp | 2 +- opentrack/plugin-api.hpp | 40 +++++++++++++++++++----- opentrack/plugin-support.hpp | 2 +- opentrack/selected-libraries.hpp | 9 +++++- opentrack/shortcuts.h | 14 ++++++--- opentrack/state.hpp | 2 +- opentrack/tracker.h | 10 ++++-- opentrack/win32-shortcuts.h | 8 ++++- proto-fg/ftnoir_protocol_fg.h | 2 +- proto-fsuipc/ftnoir_protocol_fsuipc.h | 2 +- proto-ft/ftnoir_protocol_ft.h | 2 +- proto-ftn/ftnoir_protocol_ftn.h | 2 +- proto-mouse/ftnoir_protocol_mouse.h | 2 +- proto-sc/ftnoir_protocol_sc.h | 2 +- spline-widget/CMakeLists.txt | 1 + spline-widget/qfunctionconfigurator.cpp | 2 +- tracker-aruco/ftnoir_tracker_aruco.h | 2 +- tracker-freepie-udp/ftnoir_tracker_freepie-udp.h | 2 +- tracker-hatire/ftnoir_tracker_hat_settings.cpp | 2 +- tracker-ht/ftnoir_tracker_ht.h | 2 +- tracker-hydra/ftnoir_tracker_hydra.h | 2 +- tracker-joystick/ftnoir_tracker_joystick.h | 2 +- tracker-pt/ftnoir_tracker_pt_settings.h | 2 +- tracker-rift-025/ftnoir_tracker_rift_025.h | 2 +- tracker-rift-042/ftnoir_tracker_rift_042.h | 2 +- tracker-rift-080/ftnoir_tracker_rift_080.h | 2 +- tracker-udp/ftnoir_tracker_udp.h | 2 +- 42 files changed, 157 insertions(+), 78 deletions(-) create mode 100644 opentrack-compat/compat-export.hpp create mode 100644 opentrack-compat/compat-import.hpp create mode 100644 opentrack-compat/import.hpp (limited to 'opentrack-compat') diff --git a/filter-accela/ftnoir_filter_accela.h b/filter-accela/ftnoir_filter_accela.h index b15e0cea..289b84f0 100644 --- a/filter-accela/ftnoir_filter_accela.h +++ b/filter-accela/ftnoir_filter_accela.h @@ -12,7 +12,7 @@ #include #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; #include "opentrack-compat/timer.hpp" diff --git a/filter-ewma2/ftnoir_filter_ewma2.h b/filter-ewma2/ftnoir_filter_ewma2.h index bf4e83ad..bdb9cedc 100644 --- a/filter-ewma2/ftnoir_filter_ewma2.h +++ b/filter-ewma2/ftnoir_filter_ewma2.h @@ -5,7 +5,7 @@ #include #include #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; struct settings : opts { diff --git a/filter-kalman/ftnoir_filter_kalman.h b/filter-kalman/ftnoir_filter_kalman.h index a6f40bb7..773f61d5 100644 --- a/filter-kalman/ftnoir_filter_kalman.h +++ b/filter-kalman/ftnoir_filter_kalman.h @@ -16,7 +16,7 @@ #include #include #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; struct settings : opts { diff --git a/gui/main.cpp b/gui/main.cpp index a63fe54a..3ae01404 100644 --- a/gui/main.cpp +++ b/gui/main.cpp @@ -3,7 +3,7 @@ #endif #include "ui.h" -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; #include #include diff --git a/gui/new_file_dialog.h b/gui/new_file_dialog.h index 3a35cf71..5fdabb3a 100644 --- a/gui/new_file_dialog.h +++ b/gui/new_file_dialog.h @@ -1,7 +1,7 @@ #pragma once #include "ui_new_config.h" -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" #include #include #include diff --git a/gui/process_detector.h b/gui/process_detector.h index f6497c90..e395e1a2 100644 --- a/gui/process_detector.h +++ b/gui/process_detector.h @@ -13,7 +13,7 @@ #include #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; class FancyTable : public QTableWidget diff --git a/gui/ui.cpp b/gui/ui.cpp index 5ea5dbfa..fc6d4754 100644 --- a/gui/ui.cpp +++ b/gui/ui.cpp @@ -8,7 +8,7 @@ #include "ui.h" #include "opentrack/tracker.h" -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" #include "new_file_dialog.h" #include #include @@ -411,7 +411,7 @@ bool mk_dialog(mem lib, mem& orig) dialog->show(); dialog->raise(); - QObject::connect(dialog.get(), &BaseDialog::closing, [&]() -> void { orig = nullptr; }); + QObject::connect(dialog.get(), &plugin_api::detail::BaseDialog::closing, [&]() -> void { orig = nullptr; }); return true; } diff --git a/gui/ui.h b/gui/ui.h index 91e4ebbf..44bcde0c 100644 --- a/gui/ui.h +++ b/gui/ui.h @@ -19,7 +19,7 @@ #include "ui_main.h" -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" #include "opentrack/main-settings.hpp" #include "opentrack/plugin-support.hpp" #include "opentrack/tracker.h" diff --git a/opentrack-compat/compat-export.hpp b/opentrack-compat/compat-export.hpp new file mode 100644 index 00000000..2d6f1d3d --- /dev/null +++ b/opentrack-compat/compat-export.hpp @@ -0,0 +1,15 @@ +#pragma once + +#ifndef OPENTRACK_COMPAT_EXPORT +# ifdef _WIN32 +# define OPENTRACK_COMPAT_LINKAGE __declspec(dllexport) +# else +# define OPENTRACK_COMPAT_LINKAGE +# endif + +# ifndef _MSC_VER +# define OPENTRACK_COMPAT_EXPORT __attribute__ ((visibility ("default"))) OPENTRACK_COMPAT_LINKAGE +# else +# define OPENTRACK_COMPAT_EXPORT OPENTRACK_COMPAT_LINKAGE +# endif +#endif diff --git a/opentrack-compat/compat-import.hpp b/opentrack-compat/compat-import.hpp new file mode 100644 index 00000000..161ccc5d --- /dev/null +++ b/opentrack-compat/compat-import.hpp @@ -0,0 +1,15 @@ +#pragma once + +#ifndef OPENTRACK_COMPAT_EXPORT +# ifdef _WIN32 +# define OPENTRACK_COMPAT_LINKAGE __declspec(dllimport) +# else +# define OPENTRACK_COMPAT_LINKAGE +# endif + +# ifndef _MSC_VER +# define OPENTRACK_COMPAT_EXPORT __attribute__ ((visibility ("default"))) OPENTRACK_COMPAT_LINKAGE +# else +# define OPENTRACK_COMPAT_EXPORT OPENTRACK_COMPAT_LINKAGE +# endif +#endif diff --git a/opentrack-compat/export.hpp b/opentrack-compat/export.hpp index f0983b75..100950a0 100644 --- a/opentrack-compat/export.hpp +++ b/opentrack-compat/export.hpp @@ -1,13 +1,15 @@ #pragma once -#ifdef _WIN32 -# define OPENTRACK_LINKAGE __declspec(dllexport) -#else -# define OPENTRACK_LINKAGE -#endif +#ifndef OPENTRACK_EXPORT +# ifdef _WIN32 +# define OPENTRACK_LINKAGE __declspec(dllexport) +# else +# define OPENTRACK_LINKAGE +# endif -#ifndef _MSC_VER -# define OPENTRACK_EXPORT __attribute__ ((visibility ("default"))) OPENTRACK_LINKAGE -#else -# define OPENTRACK_EXPORT OPENTRACK_LINKAGE -#endif \ No newline at end of file +# ifndef _MSC_VER +# define OPENTRACK_EXPORT __attribute__ ((visibility ("default"))) OPENTRACK_LINKAGE +# else +# define OPENTRACK_EXPORT OPENTRACK_LINKAGE +# endif +#endif diff --git a/opentrack-compat/import.hpp b/opentrack-compat/import.hpp new file mode 100644 index 00000000..3747b141 --- /dev/null +++ b/opentrack-compat/import.hpp @@ -0,0 +1,15 @@ +#pragma once + +#ifndef OPENTRACK_EXPORT +# ifdef _WIN32 +# define OPENTRACK_LINKAGE __declspec(dllimport) +# else +# define OPENTRACK_LINKAGE +# endif + +# ifndef _MSC_VER +# define OPENTRACK_EXPORT __attribute__ ((visibility ("default"))) OPENTRACK_LINKAGE +# else +# define OPENTRACK_EXPORT OPENTRACK_LINKAGE +# endif +#endif diff --git a/opentrack-compat/shm.h b/opentrack-compat/shm.h index 28bda986..c62976bd 100644 --- a/opentrack-compat/shm.h +++ b/opentrack-compat/shm.h @@ -22,29 +22,13 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wattributes" -#ifdef __GNUC__ -# define COMPAT_GNUC_VISIBILITY __attribute__ ((visibility ("default"))) -#else -# define COMPAT_GNUC_VISIBILITY -#endif - #ifdef BUILD_compat -# ifdef _WIN32 -# define COMPAT_WIN32_EXPORT __declspec(dllexport) -# else -# define COMPAT_WIN32_EXPORT -# endif +# include "compat-export.hpp" #else -# ifdef _WIN32 -# define COMPAT_WIN32_EXPORT __declspec(dllimport) -# else -# define COMPAT_WIN32_EXPORT -# endif +# include "compat-import.hpp" #endif -#define COMPAT_EXPORT COMPAT_WIN32_EXPORT COMPAT_GNUC_VISIBILITY - -class COMPAT_EXPORT PortableLockedShm { +class OPENTRACK_COMPAT_EXPORT PortableLockedShm { public: PortableLockedShm(const char *shmName, const char *mutexName, int mapSize); ~PortableLockedShm(); diff --git a/opentrack/CMakeLists.txt b/opentrack/CMakeLists.txt index c3676979..de5975e2 100644 --- a/opentrack/CMakeLists.txt +++ b/opentrack/CMakeLists.txt @@ -1,4 +1,4 @@ -opentrack_boilerplate(opentrack-api NO-LINKER-SCRIPT NO-COMPAT) +opentrack_boilerplate(opentrack-api NO-LINKER-SCRIPT NO-COMPAT LINKAGE) if(NOT WIN32) target_link_libraries(opentrack-api opentrack-qxt-mini opentrack-compat dl) else() diff --git a/opentrack/main-settings.hpp b/opentrack/main-settings.hpp index 613223ce..b7b086ad 100644 --- a/opentrack/main-settings.hpp +++ b/opentrack/main-settings.hpp @@ -9,7 +9,7 @@ #pragma once #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" #include "opentrack/plugin-api.hpp" using namespace options; diff --git a/opentrack/mappings.hpp b/opentrack/mappings.hpp index bb38a3ca..087ea7f3 100644 --- a/opentrack/mappings.hpp +++ b/opentrack/mappings.hpp @@ -8,7 +8,7 @@ #pragma once #include -#include "options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; #include "spline-widget/functionconfig.h" #include "main-settings.hpp" diff --git a/opentrack/plugin-api.hpp b/opentrack/plugin-api.hpp index a57077ab..5fdc3bcb 100644 --- a/opentrack/plugin-api.hpp +++ b/opentrack/plugin-api.hpp @@ -8,17 +8,38 @@ #pragma once -#include "../opentrack-compat/export.hpp" #include #include #include #include +#ifdef BUILD_api +# include "opentrack-compat/export.hpp" +#else +# include "opentrack-compat/import.hpp" +#endif + +#ifndef OPENTRACK_PLUGIN_EXPORT +# ifdef _WIN32 +# define OPENTRACK_PLUGIN_LINKAGE __declspec(dllexport) +# else +# define OPENTRACK_PLUGIN_LINKAGE +# endif +# ifndef _MSC_VER +# define OPENTRACK_PLUGIN_EXPORT __attribute__ ((visibility ("default"))) OPENTRACK_PLUGIN_LINKAGE +# else +# define OPENTRACK_PLUGIN_EXPORT OPENTRACK_PLUGIN_LINKAGE +# endif +#endif + enum Axis { TX = 0, TY, TZ, Yaw, Pitch, Roll }; -class BaseDialog : public QWidget +namespace plugin_api { +namespace detail { + +class OPENTRACK_EXPORT BaseDialog : public QWidget { Q_OBJECT public: @@ -27,16 +48,19 @@ signals: void closing(); }; +} // ns +} // ns + #define OPENTRACK_DECLARE_PLUGIN_INTERNAL(ctor_class, ctor_ret_class, metadata_class, dialog_class, dialog_ret_class) \ - extern "C" OPENTRACK_EXPORT ctor_ret_class* GetConstructor() \ + extern "C" OPENTRACK_PLUGIN_EXPORT ctor_ret_class* GetConstructor() \ { \ return new ctor_class; \ } \ - extern "C" OPENTRACK_EXPORT Metadata* GetMetadata() \ + extern "C" OPENTRACK_PLUGIN_EXPORT Metadata* GetMetadata() \ { \ return new metadata_class; \ } \ - extern "C" OPENTRACK_EXPORT dialog_ret_class* GetDialog() \ + extern "C" OPENTRACK_PLUGIN_EXPORT dialog_ret_class* GetDialog() \ { \ return new dialog_class; \ } @@ -65,7 +89,7 @@ struct IFilter virtual void center() {} }; -struct IFilterDialog : public BaseDialog +struct IFilterDialog : public plugin_api::detail::BaseDialog { // optional destructor virtual ~IFilterDialog() {} @@ -93,7 +117,7 @@ struct IProtocol virtual QString game_name() = 0; }; -struct IProtocolDialog : public BaseDialog +struct IProtocolDialog : public plugin_api::detail::BaseDialog { // optional destructor virtual ~IProtocolDialog() {} @@ -118,7 +142,7 @@ struct ITracker virtual void data(double *data) = 0; }; -struct ITrackerDialog : public BaseDialog +struct ITrackerDialog : public plugin_api::detail::BaseDialog { // optional destructor virtual ~ITrackerDialog() {} diff --git a/opentrack/plugin-support.hpp b/opentrack/plugin-support.hpp index 102d11c4..650eec10 100644 --- a/opentrack/plugin-support.hpp +++ b/opentrack/plugin-support.hpp @@ -8,7 +8,7 @@ #pragma once #include "plugin-api.hpp" -#include "options.hpp" +#include "opentrack-compat/options.hpp" #include #include diff --git a/opentrack/selected-libraries.hpp b/opentrack/selected-libraries.hpp index 3719b109..b396b5ec 100644 --- a/opentrack/selected-libraries.hpp +++ b/opentrack/selected-libraries.hpp @@ -11,7 +11,14 @@ #include "opentrack/plugin-support.hpp" #include -struct SelectedLibraries { + +#ifdef BUILD_api +# include "opentrack-compat/export.hpp" +#else +# include "opentrack-compat/import.hpp" +#endif + +struct OPENTRACK_EXPORT SelectedLibraries { using dylibptr = mem; mem pTracker; mem pFilter; diff --git a/opentrack/shortcuts.h b/opentrack/shortcuts.h index 63d91829..03adc1ab 100644 --- a/opentrack/shortcuts.h +++ b/opentrack/shortcuts.h @@ -17,9 +17,15 @@ #include #include +#ifdef BUILD_api +# include "opentrack-compat/export.hpp" +#else +# include "opentrack-compat/import.hpp" +#endif + #include "qxt-mini/QxtGlobalShortcut" #include "opentrack/plugin-support.hpp" -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" #include "opentrack/main-settings.hpp" using namespace options; @@ -66,9 +72,7 @@ typedef unsigned char BYTE; struct Key { int foo; }; #endif -struct Shortcuts; - -struct KeybindingWorker : public QThread { +struct OPENTRACK_EXPORT KeybindingWorker : public QThread { #ifdef _WIN32 private: LPDIRECTINPUT8 din; @@ -87,7 +91,7 @@ public: #endif }; -struct Shortcuts : public QObject { +struct OPENTRACK_EXPORT Shortcuts : public QObject { Q_OBJECT public: diff --git a/opentrack/state.hpp b/opentrack/state.hpp index e4cb0f04..dcb18293 100644 --- a/opentrack/state.hpp +++ b/opentrack/state.hpp @@ -9,7 +9,7 @@ #pragma once #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; #include "opentrack/plugin-support.hpp" #include "opentrack/main-settings.hpp" diff --git a/opentrack/tracker.h b/opentrack/tracker.h index 890660e1..e26c3f7a 100644 --- a/opentrack/tracker.h +++ b/opentrack/tracker.h @@ -18,7 +18,7 @@ #include "spline-widget/functionconfig.h" #include "main-settings.hpp" -#include "options.hpp" +#include "opentrack-compat/options.hpp" #include #include @@ -40,7 +40,13 @@ public: inline double operator()(int i) const { return axes[i]; } }; -class Tracker : private QThread { +#ifdef BUILD_api +# include "opentrack-compat/export.hpp" +#else +# include "opentrack-compat/import.hpp" +#endif + +class OPENTRACK_EXPORT Tracker : private QThread { Q_OBJECT private: QMutex mtx; diff --git a/opentrack/win32-shortcuts.h b/opentrack/win32-shortcuts.h index fe92ae53..9b2c6121 100644 --- a/opentrack/win32-shortcuts.h +++ b/opentrack/win32-shortcuts.h @@ -10,7 +10,13 @@ struct win_key; extern QList windows_key_mods; extern QList windows_key_sequences; -struct win_key +#ifdef BUILD_api +# include "opentrack-compat/export.hpp" +#else +# include "opentrack-compat/import.hpp" +#endif + +struct OPENTRACK_EXPORT win_key { win_key(int win, Qt::Key qt) : win(win), qt(qt) {} int win; diff --git a/proto-fg/ftnoir_protocol_fg.h b/proto-fg/ftnoir_protocol_fg.h index de528270..5b1cbc81 100644 --- a/proto-fg/ftnoir_protocol_fg.h +++ b/proto-fg/ftnoir_protocol_fg.h @@ -15,7 +15,7 @@ #include #include #include "opentrack/plugin-api.hpp" -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; struct settings : opts { diff --git a/proto-fsuipc/ftnoir_protocol_fsuipc.h b/proto-fsuipc/ftnoir_protocol_fsuipc.h index c8e91a42..af59e988 100644 --- a/proto-fsuipc/ftnoir_protocol_fsuipc.h +++ b/proto-fsuipc/ftnoir_protocol_fsuipc.h @@ -24,7 +24,7 @@ #include #include #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; #define FSUIPC_FILENAME "C:\\Program Files\\Microsoft Games\\Flight Simulator 9\\Modules\\FSUIPC.dll" diff --git a/proto-ft/ftnoir_protocol_ft.h b/proto-ft/ftnoir_protocol_ft.h index 8e50f1cb..9cf16f03 100644 --- a/proto-ft/ftnoir_protocol_ft.h +++ b/proto-ft/ftnoir_protocol_ft.h @@ -19,7 +19,7 @@ #include #include #include "opentrack-compat/shm.h" -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" #include "freetrackclient/fttypes.h" using namespace options; diff --git a/proto-ftn/ftnoir_protocol_ftn.h b/proto-ftn/ftnoir_protocol_ftn.h index 7fe6c225..d8b8aff0 100644 --- a/proto-ftn/ftnoir_protocol_ftn.h +++ b/proto-ftn/ftnoir_protocol_ftn.h @@ -16,7 +16,7 @@ #include #include #include "opentrack/plugin-api.hpp" -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; struct settings : opts { diff --git a/proto-mouse/ftnoir_protocol_mouse.h b/proto-mouse/ftnoir_protocol_mouse.h index 595be393..f1730777 100644 --- a/proto-mouse/ftnoir_protocol_mouse.h +++ b/proto-mouse/ftnoir_protocol_mouse.h @@ -10,7 +10,7 @@ #include "ui_ftnoir_mousecontrols.h" #include #include "opentrack/plugin-api.hpp" -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; struct settings : opts { diff --git a/proto-sc/ftnoir_protocol_sc.h b/proto-sc/ftnoir_protocol_sc.h index 671a3500..dcc0ca77 100644 --- a/proto-sc/ftnoir_protocol_sc.h +++ b/proto-sc/ftnoir_protocol_sc.h @@ -20,7 +20,7 @@ #include #include #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; #include diff --git a/spline-widget/CMakeLists.txt b/spline-widget/CMakeLists.txt index 4d951f92..651a7874 100644 --- a/spline-widget/CMakeLists.txt +++ b/spline-widget/CMakeLists.txt @@ -1 +1,2 @@ opentrack_boilerplate(opentrack-spline-widget NO-LINKER-SCRIPT NO-COMPAT LINKAGE) +target_link_libraries(opentrack-spline-widget opentrack-compat) diff --git a/spline-widget/qfunctionconfigurator.cpp b/spline-widget/qfunctionconfigurator.cpp index 7ab1e360..5d910826 100644 --- a/spline-widget/qfunctionconfigurator.cpp +++ b/spline-widget/qfunctionconfigurator.cpp @@ -5,7 +5,7 @@ * copyright notice and this permission notice appear in all copies. */ -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; #include "spline-widget/qfunctionconfigurator.h" #include diff --git a/tracker-aruco/ftnoir_tracker_aruco.h b/tracker-aruco/ftnoir_tracker_aruco.h index f827da77..018f69ea 100644 --- a/tracker-aruco/ftnoir_tracker_aruco.h +++ b/tracker-aruco/ftnoir_tracker_aruco.h @@ -15,7 +15,7 @@ #include #include #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" #include "trans_calib.h" #include "opentrack/plugin-api.hpp" #include "opentrack/opencv-camera-dialog.hpp" diff --git a/tracker-freepie-udp/ftnoir_tracker_freepie-udp.h b/tracker-freepie-udp/ftnoir_tracker_freepie-udp.h index 7cff2ec9..9d18c7d1 100644 --- a/tracker-freepie-udp/ftnoir_tracker_freepie-udp.h +++ b/tracker-freepie-udp/ftnoir_tracker_freepie-udp.h @@ -10,7 +10,7 @@ #include #include "ui_freepie-udp-controls.h" #include "opentrack/plugin-api.hpp" -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; struct settings : opts { diff --git a/tracker-hatire/ftnoir_tracker_hat_settings.cpp b/tracker-hatire/ftnoir_tracker_hat_settings.cpp index e6b32047..df0480a1 100644 --- a/tracker-hatire/ftnoir_tracker_hat_settings.cpp +++ b/tracker-hatire/ftnoir_tracker_hat_settings.cpp @@ -14,7 +14,7 @@ #include "ftnoir_tracker_hat_settings.h" #ifdef OPENTRACK_API -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" #endif void TrackerSettings::load_ini() diff --git a/tracker-ht/ftnoir_tracker_ht.h b/tracker-ht/ftnoir_tracker_ht.h index 1e364456..0563fe1c 100644 --- a/tracker-ht/ftnoir_tracker_ht.h +++ b/tracker-ht/ftnoir_tracker_ht.h @@ -12,7 +12,7 @@ #include "ht_video_widget.h" #include "opentrack-compat/shm.h" #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" #include "opentrack/plugin-api.hpp" #include "opentrack/opencv-camera-dialog.hpp" diff --git a/tracker-hydra/ftnoir_tracker_hydra.h b/tracker-hydra/ftnoir_tracker_hydra.h index 47a1eb9a..94396b7c 100644 --- a/tracker-hydra/ftnoir_tracker_hydra.h +++ b/tracker-hydra/ftnoir_tracker_hydra.h @@ -1,6 +1,6 @@ #include "ui_ftnoir_hydra_clientcontrols.h" #include "opentrack/plugin-api.hpp" -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; struct settings : opts { diff --git a/tracker-joystick/ftnoir_tracker_joystick.h b/tracker-joystick/ftnoir_tracker_joystick.h index 75305f07..29e65af1 100644 --- a/tracker-joystick/ftnoir_tracker_joystick.h +++ b/tracker-joystick/ftnoir_tracker_joystick.h @@ -27,7 +27,7 @@ #include #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; struct settings : opts { diff --git a/tracker-pt/ftnoir_tracker_pt_settings.h b/tracker-pt/ftnoir_tracker_pt_settings.h index 85eec8f9..e4bfa371 100644 --- a/tracker-pt/ftnoir_tracker_pt_settings.h +++ b/tracker-pt/ftnoir_tracker_pt_settings.h @@ -9,7 +9,7 @@ #ifndef FTNOIR_TRACKER_PT_SETTINGS_H #define FTNOIR_TRACKER_PT_SETTINGS_H -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; struct settings_pt : opts diff --git a/tracker-rift-025/ftnoir_tracker_rift_025.h b/tracker-rift-025/ftnoir_tracker_rift_025.h index 2bd5024b..8333e092 100644 --- a/tracker-rift-025/ftnoir_tracker_rift_025.h +++ b/tracker-rift-025/ftnoir_tracker_rift_025.h @@ -6,7 +6,7 @@ #include "opentrack/plugin-api.hpp" #include "OVR.h" #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; struct settings : opts { diff --git a/tracker-rift-042/ftnoir_tracker_rift_042.h b/tracker-rift-042/ftnoir_tracker_rift_042.h index 437a2a39..77e7ffa6 100644 --- a/tracker-rift-042/ftnoir_tracker_rift_042.h +++ b/tracker-rift-042/ftnoir_tracker_rift_042.h @@ -6,7 +6,7 @@ #include "opentrack/plugin-api.hpp" #include "OVR.h" #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; struct settings : opts { diff --git a/tracker-rift-080/ftnoir_tracker_rift_080.h b/tracker-rift-080/ftnoir_tracker_rift_080.h index 08684dd2..879d31d2 100644 --- a/tracker-rift-080/ftnoir_tracker_rift_080.h +++ b/tracker-rift-080/ftnoir_tracker_rift_080.h @@ -6,7 +6,7 @@ #include "opentrack/plugin-api.hpp" #include "OVR.h" #include -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; struct settings : opts { diff --git a/tracker-udp/ftnoir_tracker_udp.h b/tracker-udp/ftnoir_tracker_udp.h index 6de5b295..3f0e6d8c 100644 --- a/tracker-udp/ftnoir_tracker_udp.h +++ b/tracker-udp/ftnoir_tracker_udp.h @@ -4,7 +4,7 @@ #include #include #include "opentrack/plugin-api.hpp" -#include "opentrack/options.hpp" +#include "opentrack-compat/options.hpp" using namespace options; struct settings : opts { -- cgit v1.2.3 From be36337e46218c6f843a478036497a3f494502ed Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 1 Nov 2015 11:29:24 +0100 Subject: options: don't define options singleton in every module Instead, define in opentrack-api.so only. Also, move to opentrack-compat to break a circular dependency --- opentrack-compat/options.cpp | 16 ++ opentrack-compat/options.hpp | 485 +++++++++++++++++++++++++++++++++++++++++++ opentrack/options.hpp | 481 ------------------------------------------ 3 files changed, 501 insertions(+), 481 deletions(-) create mode 100644 opentrack-compat/options.cpp create mode 100644 opentrack-compat/options.hpp delete mode 100644 opentrack/options.hpp (limited to 'opentrack-compat') diff --git a/opentrack-compat/options.cpp b/opentrack-compat/options.cpp new file mode 100644 index 00000000..91cd3664 --- /dev/null +++ b/opentrack-compat/options.cpp @@ -0,0 +1,16 @@ +#include "options.hpp" + +namespace options +{ + +namespace detail +{ +OPENTRACK_COMPAT_EXPORT opt_singleton& singleton() +{ + static auto ret = std::make_shared(); + return *ret; +} + +} + +} diff --git a/opentrack-compat/options.hpp b/opentrack-compat/options.hpp new file mode 100644 index 00000000..be8c688c --- /dev/null +++ b/opentrack-compat/options.hpp @@ -0,0 +1,485 @@ +/* Copyright (c) 2013-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 +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include + +#ifdef BUILD_compat +# include "compat-export.hpp" +#else +# include "compat-import.hpp" +#endif + +template using mem = std::shared_ptr; + +#define OPENTRACK_CONFIG_FILENAME_KEY "settings-filename" +#define OPENTRACK_DEFAULT_CONFIG "default.ini" +#define OPENTRACK_ORG "opentrack-2.3" + +namespace options { + template + using map = std::map; + using std::string; + + template + // don't elide usages of the function, qvariant default implicit + // conversion results in nonsensical runtime behavior -sh + inline t qcruft_to_t (const QVariant& datum); + + template<> + inline int qcruft_to_t(const QVariant& t) + { + return t.toInt(); + } + + template<> + inline QString qcruft_to_t(const QVariant& t) + { + return t.toString(); + } + + template<> + inline bool qcruft_to_t(const QVariant& t) + { + return t.toBool(); + } + + template<> + inline double qcruft_to_t(const QVariant& t) + { + return t.toDouble(); + } + + template<> + inline QVariant qcruft_to_t(const QVariant& t) + { + return t; + } + + // snapshot of qsettings group at given time + class group { + private: + map kvs; + string name; + public: + group(const string& name) : name(name) + { + auto conf = ini_file(); + auto q_name = QString::fromStdString(name); + conf->beginGroup(q_name); + for (auto& k_ : conf->childKeys()) + { + auto tmp = k_.toUtf8(); + string k(tmp); + kvs[k] = conf->value(k_); + } + conf->endGroup(); + } + + void save() + { + auto s = ini_file(); + auto q_name = QString::fromStdString(name); + s->beginGroup(q_name); + for (auto& i : kvs) + { + auto k = QString::fromStdString(i.first); + s->setValue(k, i.second); + } + s->endGroup(); + s->sync(); + } + + template + t get(const string& k) + { + return qcruft_to_t(kvs[k]); + } + + void put(const string& s, const QVariant& d) + { + kvs[s] = d; + } + + bool contains(const string& s) + { + return kvs.count(s) != 0; + } + + static QString ini_directory() + { + const auto dirs = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); + if (dirs.size() == 0) + return ""; + if (QDir(dirs[0]).mkpath(OPENTRACK_ORG)) + return dirs[0] + "/" OPENTRACK_ORG; + return ""; + } + + static QString ini_filename() + { + QSettings settings(OPENTRACK_ORG); + return settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); + } + + static QString ini_pathname() + { + const auto dir = ini_directory(); + if (dir == "") + return ""; + QSettings settings(OPENTRACK_ORG); + return dir + "/" + settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); + } + + static const QStringList ini_list() + { + const auto dirname = ini_directory(); + if (dirname == "") + return QStringList(); + QDir settings_dir(dirname); + return settings_dir.entryList( QStringList { "*.ini" } , QDir::Files, QDir::Name ); + } + + static const mem ini_file() + { + const auto pathname = ini_pathname(); + if (pathname != "") + return std::make_shared(ini_pathname(), QSettings::IniFormat); + return std::make_shared(); + } + }; + + class OPENTRACK_COMPAT_EXPORT impl_bundle : public QObject { + Q_OBJECT + protected: + QMutex mtx; + const string group_name; + group saved; + group transient; + bool modified; + impl_bundle(const impl_bundle&) = delete; + impl_bundle& operator=(const impl_bundle&) = delete; + signals: + void reloading(); + void saving(); + public: + impl_bundle(const string& group_name) : + mtx(QMutex::Recursive), + group_name(group_name), + saved(group_name), + transient(saved), + modified(false) + { + } + + string name() { return group_name; } + + void reload() { + { + QMutexLocker l(&mtx); + saved = group(group_name); + transient = saved; + modified = false; + } + emit reloading(); + } + + void store_kv(const string& name, const QVariant& datum) + { + QMutexLocker l(&mtx); + + auto old = transient.get(name); + if (!transient.contains(name) || datum != old) + { + modified = true; + transient.put(name, datum); + } + } + bool contains(const string& name) + { + QMutexLocker l(&mtx); + return transient.contains(name); + } + template + t get(const string& name) + { + QMutexLocker l(&mtx); + return transient.get(name); + } + void save() + { + { + QMutexLocker l(&mtx); + modified = false; + saved = transient; + transient.save(); + } + emit saving(); + } + + bool modifiedp() { + QMutexLocker l(&mtx); + return modified; + } + }; + + class opt_bundle; + + namespace detail + { + struct OPENTRACK_COMPAT_EXPORT opt_singleton + { + public: + using k = std::string; + using v = opt_bundle; + using cnt = int; + using pbundle = std::shared_ptr; + using tt = std::tuple>; + private: + QMutex implsgl_mtx; + map implsgl_data; + public: + opt_singleton() : implsgl_mtx(QMutex::Recursive) {} + + pbundle bundle(const k& key) + { + QMutexLocker l(&implsgl_mtx); + + if (implsgl_data.count(key) != 0) + { + auto shared = std::get<1>(implsgl_data[key]).lock(); + if (shared != nullptr) + return shared; + } + + qDebug() << "bundle +" << QString::fromStdString(key); + + auto shr = std::make_shared(key); + implsgl_data[key] = tt(cnt(1), shr); + return shr; + } + + void bundle_decf(const k& key) + { + QMutexLocker l(&implsgl_mtx); + + if (--std::get<0>(implsgl_data[key]) == 0) + implsgl_data.erase(key); + } + }; + + OPENTRACK_COMPAT_EXPORT opt_singleton& singleton(); + } + + using pbundle = std::shared_ptr; + + static inline pbundle bundle(const string name) { return detail::singleton().bundle(name); } + + class opt_bundle : public impl_bundle + { + public: + opt_bundle() : impl_bundle("i-have-no-name") {} + opt_bundle(const string& group_name) : impl_bundle(group_name) + { + } + + ~opt_bundle() + { + qDebug() << "bundle -" << QString::fromStdString(group_name); + detail::singleton().bundle_decf(group_name); + } + }; + + class OPENTRACK_COMPAT_EXPORT base_value : public QObject + { + Q_OBJECT +#define DEFINE_SLOT(t) void setValue(t datum) { store(datum); } +#define DEFINE_SIGNAL(t) void valueChanged(t) + public: + string name() { return self_name; } + base_value(pbundle b, const string& name) : b(b), self_name(name) {} + signals: + DEFINE_SIGNAL(double); + DEFINE_SIGNAL(int); + DEFINE_SIGNAL(bool); + DEFINE_SIGNAL(QString); + protected: + pbundle b; + string self_name; + + template + void store(const t& datum) + { + b->store_kv(self_name, datum); + emit valueChanged(static_cast(datum)); + } + public slots: + DEFINE_SLOT(double) + DEFINE_SLOT(int) + DEFINE_SLOT(QString) + DEFINE_SLOT(bool) + public slots: + virtual void reload() = 0; + }; + + static inline string string_from_qstring(const QString& datum) + { + auto tmp = datum.toUtf8(); + return string(tmp.constData()); + } + + template + class value : public base_value { + public: + t operator=(const t datum) + { + store(datum); + return datum; + } + static constexpr const Qt::ConnectionType DIRECT_CONNTYPE = Qt::DirectConnection; + static constexpr const Qt::ConnectionType SAFE_CONNTYPE = Qt::UniqueConnection; + value(pbundle b, const string& name, t def) : base_value(b, name), def(def) + { + QObject::connect(b.get(), SIGNAL(reloading()), + this, SLOT(reload()), + DIRECT_CONNTYPE); + if (!b->contains(name) || b->get(name).type() == QVariant::Invalid) + *this = def; + } + value(pbundle b, const QString& name, t def) : value(b, string_from_qstring(name), def) {} + value(pbundle b, const char* name, t def) : value(b, string(name), def) {} + + operator t() + { + return b->contains(self_name) ? b->get(self_name) : def; + } + void reload() override { + *this = static_cast(*this); + } + private: + t def; + }; + + struct opts + { + pbundle b; + + opts(const std::string& name) : b(bundle(name)) {} + + ~opts() + { + b->reload(); + } + }; + + template + inline void tie_setting(value&, q*); + + template<> + inline void tie_setting(value& v, QComboBox* cb) + { + cb->setCurrentIndex(v); + v = cb->currentIndex(); + base_value::connect(cb, SIGNAL(currentIndexChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(int)), cb, SLOT(setCurrentIndex(int)), v.SAFE_CONNTYPE); + } + + template<> + inline void tie_setting(value& v, QComboBox* cb) + { + cb->setCurrentText(v); + v = cb->currentText(); + base_value::connect(cb, SIGNAL(currentTextChanged(QString)), &v, SLOT(setValue(QString)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(QString)), cb, SLOT(setCurrentText(QString)), v.SAFE_CONNTYPE); + } + + template<> + inline void tie_setting(value& v, QCheckBox* cb) + { + cb->setChecked(v); + base_value::connect(cb, SIGNAL(toggled(bool)), &v, SLOT(setValue(bool)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(bool)), cb, SLOT(setChecked(bool)), v.SAFE_CONNTYPE); + } + + template<> + inline void tie_setting(value& v, QDoubleSpinBox* dsb) + { + dsb->setValue(v); + base_value::connect(dsb, SIGNAL(valueChanged(double)), &v, SLOT(setValue(double)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(double)), dsb, SLOT(setValue(double)), v.SAFE_CONNTYPE); + } + + template<> + inline void tie_setting(value& v, QSpinBox* sb) + { + sb->setValue(v); + base_value::connect(sb, SIGNAL(valueChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(int)), sb, SLOT(setValue(int)), v.SAFE_CONNTYPE); + } + + template<> + inline void tie_setting(value& v, QSlider* sl) + { + sl->setValue(v); + v = sl->value(); + base_value::connect(sl, SIGNAL(valueChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(int)), sl, SLOT(setValue(int)), v.SAFE_CONNTYPE); + } + + template<> + inline void tie_setting(value& v, QLineEdit* le) + { + le->setText(v); + base_value::connect(le, SIGNAL(textChanged(QString)), &v, SLOT(setValue(QString)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(QString)),le, SLOT(setText(QString)), v.SAFE_CONNTYPE); + } + + template<> + inline void tie_setting(value& v, QLabel* lb) + { + lb->setText(v); + base_value::connect(&v, SIGNAL(valueChanged(QString)), lb, SLOT(setText(QString)), v.SAFE_CONNTYPE); + } + + template<> + inline void tie_setting(value& v, QTabWidget* t) + { + t->setCurrentIndex(v); + base_value::connect(t, SIGNAL(currentChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(int)), t, SLOT(setCurrentIndex(int)), v.SAFE_CONNTYPE); + } +} diff --git a/opentrack/options.hpp b/opentrack/options.hpp deleted file mode 100644 index f8475877..00000000 --- a/opentrack/options.hpp +++ /dev/null @@ -1,481 +0,0 @@ -/* Copyright (c) 2013-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 -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include -template using mem = std::shared_ptr; - -#define OPENTRACK_CONFIG_FILENAME_KEY "settings-filename" -#define OPENTRACK_DEFAULT_CONFIG "default.ini" -#define OPENTRACK_ORG "opentrack-2.3" - -namespace options { - template - using map = std::map; - using std::string; - - template - // don't elide usages of the function, qvariant default implicit - // conversion results in nonsensical runtime behavior -sh - inline t qcruft_to_t (const QVariant& datum); - - template<> - inline int qcruft_to_t(const QVariant& t) - { - return t.toInt(); - } - - template<> - inline QString qcruft_to_t(const QVariant& t) - { - return t.toString(); - } - - template<> - inline bool qcruft_to_t(const QVariant& t) - { - return t.toBool(); - } - - template<> - inline double qcruft_to_t(const QVariant& t) - { - return t.toDouble(); - } - - template<> - inline QVariant qcruft_to_t(const QVariant& t) - { - return t; - } - - // snapshot of qsettings group at given time - class group { - private: - map kvs; - string name; - public: - group(const string& name) : name(name) - { - auto conf = ini_file(); - auto q_name = QString::fromStdString(name); - conf->beginGroup(q_name); - for (auto& k_ : conf->childKeys()) - { - auto tmp = k_.toUtf8(); - string k(tmp); - kvs[k] = conf->value(k_); - } - conf->endGroup(); - } - - void save() - { - auto s = ini_file(); - auto q_name = QString::fromStdString(name); - s->beginGroup(q_name); - for (auto& i : kvs) - { - auto k = QString::fromStdString(i.first); - s->setValue(k, i.second); - } - s->endGroup(); - s->sync(); - } - - template - t get(const string& k) - { - return qcruft_to_t(kvs[k]); - } - - void put(const string& s, const QVariant& d) - { - kvs[s] = d; - } - - bool contains(const string& s) - { - return kvs.count(s) != 0; - } - - static QString ini_directory() - { - const auto dirs = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); - if (dirs.size() == 0) - return ""; - if (QDir(dirs[0]).mkpath(OPENTRACK_ORG)) - return dirs[0] + "/" OPENTRACK_ORG; - return ""; - } - - static QString ini_filename() - { - QSettings settings(OPENTRACK_ORG); - return settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); - } - - static QString ini_pathname() - { - const auto dir = ini_directory(); - if (dir == "") - return ""; - QSettings settings(OPENTRACK_ORG); - return dir + "/" + settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); - } - - static const QStringList ini_list() - { - const auto dirname = ini_directory(); - if (dirname == "") - return QStringList(); - QDir settings_dir(dirname); - return settings_dir.entryList( QStringList { "*.ini" } , QDir::Files, QDir::Name ); - } - - static const mem ini_file() - { - const auto pathname = ini_pathname(); - if (pathname != "") - return std::make_shared(ini_pathname(), QSettings::IniFormat); - return std::make_shared(); - } - }; - - class impl_bundle : public QObject { - Q_OBJECT - protected: - QMutex mtx; - const string group_name; - group saved; - group transient; - bool modified; - impl_bundle(const impl_bundle&) = delete; - impl_bundle& operator=(const impl_bundle&) = delete; - signals: - void reloading(); - void saving(); - public: - impl_bundle(const string& group_name) : - mtx(QMutex::Recursive), - group_name(group_name), - saved(group_name), - transient(saved), - modified(false) - { - } - - string name() { return group_name; } - - void reload() { - { - QMutexLocker l(&mtx); - saved = group(group_name); - transient = saved; - modified = false; - } - emit reloading(); - } - - void store_kv(const string& name, const QVariant& datum) - { - QMutexLocker l(&mtx); - - auto old = transient.get(name); - if (!transient.contains(name) || datum != old) - { - modified = true; - transient.put(name, datum); - } - } - bool contains(const string& name) - { - QMutexLocker l(&mtx); - return transient.contains(name); - } - template - t get(const string& name) - { - QMutexLocker l(&mtx); - return transient.get(name); - } - void save() - { - { - QMutexLocker l(&mtx); - modified = false; - saved = transient; - transient.save(); - } - emit saving(); - } - - bool modifiedp() { - QMutexLocker l(&mtx); - return modified; - } - }; - - class opt_bundle; - - namespace - { - template - struct opt_singleton - { - public: - using pbundle = std::shared_ptr; - using tt = std::tuple>; - private: - QMutex implsgl_mtx; - map implsgl_data; - public: - opt_singleton() : implsgl_mtx(QMutex::Recursive) {} - - static opt_singleton& datum() - { - static auto ret = std::make_shared>(); - return *ret; - } - - pbundle bundle(const k& key) - { - QMutexLocker l(&implsgl_mtx); - - if (implsgl_data.count(key) != 0) - { - auto shared = std::get<1>(implsgl_data[key]).lock(); - if (shared != nullptr) - return shared; - } - - qDebug() << "bundle +" << QString::fromStdString(key); - - auto shr = std::make_shared(key); - implsgl_data[key] = tt(cnt(1), shr); - return shr; - } - - void bundle_decf(const k& key) - { - QMutexLocker l(&implsgl_mtx); - - if (--std::get<0>(implsgl_data[key]) == 0) - implsgl_data.erase(key); - } - }; - - using pbundle = std::shared_ptr; - using t_fact = opt_singleton; - } - - static inline t_fact::pbundle bundle(const string name) { return t_fact::datum().bundle(name); } - - class opt_bundle : public impl_bundle - { - public: - opt_bundle() : impl_bundle("i-have-no-name") {} - opt_bundle(const string& group_name) : impl_bundle(group_name) - { - } - - ~opt_bundle() - { - qDebug() << "bundle -" << QString::fromStdString(group_name); - t_fact::datum().bundle_decf(group_name); - } - }; - - class base_value : public QObject - { - Q_OBJECT -#define DEFINE_SLOT(t) void setValue(t datum) { store(datum); } -#define DEFINE_SIGNAL(t) void valueChanged(t) - public: - string name() { return self_name; } - base_value(pbundle b, const string& name) : b(b), self_name(name) {} - signals: - DEFINE_SIGNAL(double); - DEFINE_SIGNAL(int); - DEFINE_SIGNAL(bool); - DEFINE_SIGNAL(QString); - protected: - pbundle b; - string self_name; - - template - void store(const t& datum) - { - b->store_kv(self_name, datum); - emit valueChanged(static_cast(datum)); - } - public slots: - DEFINE_SLOT(double) - DEFINE_SLOT(int) - DEFINE_SLOT(QString) - DEFINE_SLOT(bool) - public slots: - virtual void reload() = 0; - }; - - static inline string string_from_qstring(const QString& datum) - { - auto tmp = datum.toUtf8(); - return string(tmp.constData()); - } - - template - class value : public base_value { - public: - t operator=(const t datum) - { - store(datum); - return datum; - } - static constexpr const Qt::ConnectionType DIRECT_CONNTYPE = Qt::DirectConnection; - static constexpr const Qt::ConnectionType SAFE_CONNTYPE = Qt::UniqueConnection; - value(pbundle b, const string& name, t def) : base_value(b, name), def(def) - { - QObject::connect(b.get(), SIGNAL(reloading()), - this, SLOT(reload()), - DIRECT_CONNTYPE); - if (!b->contains(name) || b->get(name).type() == QVariant::Invalid) - *this = def; - } - value(pbundle b, const QString& name, t def) : value(b, string_from_qstring(name), def) {} - value(pbundle b, const char* name, t def) : value(b, string(name), def) {} - - operator t() - { - return b->contains(self_name) ? b->get(self_name) : def; - } - void reload() override { - *this = static_cast(*this); - } - private: - t def; - }; - - struct opts - { - pbundle b; - - opts(const std::string& name) : b(bundle(name)) {} - - ~opts() - { - b->reload(); - } - }; - - template - inline void tie_setting(value&, q*); - - template<> - inline void tie_setting(value& v, QComboBox* cb) - { - cb->setCurrentIndex(v); - v = cb->currentIndex(); - base_value::connect(cb, SIGNAL(currentIndexChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); - base_value::connect(&v, SIGNAL(valueChanged(int)), cb, SLOT(setCurrentIndex(int)), v.SAFE_CONNTYPE); - } - - template<> - inline void tie_setting(value& v, QComboBox* cb) - { - cb->setCurrentText(v); - v = cb->currentText(); - base_value::connect(cb, SIGNAL(currentTextChanged(QString)), &v, SLOT(setValue(QString)), v.DIRECT_CONNTYPE); - base_value::connect(&v, SIGNAL(valueChanged(QString)), cb, SLOT(setCurrentText(QString)), v.SAFE_CONNTYPE); - } - - template<> - inline void tie_setting(value& v, QCheckBox* cb) - { - cb->setChecked(v); - base_value::connect(cb, SIGNAL(toggled(bool)), &v, SLOT(setValue(bool)), v.DIRECT_CONNTYPE); - base_value::connect(&v, SIGNAL(valueChanged(bool)), cb, SLOT(setChecked(bool)), v.SAFE_CONNTYPE); - } - - template<> - inline void tie_setting(value& v, QDoubleSpinBox* dsb) - { - dsb->setValue(v); - base_value::connect(dsb, SIGNAL(valueChanged(double)), &v, SLOT(setValue(double)), v.DIRECT_CONNTYPE); - base_value::connect(&v, SIGNAL(valueChanged(double)), dsb, SLOT(setValue(double)), v.SAFE_CONNTYPE); - } - - template<> - inline void tie_setting(value& v, QSpinBox* sb) - { - sb->setValue(v); - base_value::connect(sb, SIGNAL(valueChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); - base_value::connect(&v, SIGNAL(valueChanged(int)), sb, SLOT(setValue(int)), v.SAFE_CONNTYPE); - } - - template<> - inline void tie_setting(value& v, QSlider* sl) - { - sl->setValue(v); - v = sl->value(); - base_value::connect(sl, SIGNAL(valueChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); - base_value::connect(&v, SIGNAL(valueChanged(int)), sl, SLOT(setValue(int)), v.SAFE_CONNTYPE); - } - - template<> - inline void tie_setting(value& v, QLineEdit* le) - { - le->setText(v); - base_value::connect(le, SIGNAL(textChanged(QString)), &v, SLOT(setValue(QString)), v.DIRECT_CONNTYPE); - base_value::connect(&v, SIGNAL(valueChanged(QString)),le, SLOT(setText(QString)), v.SAFE_CONNTYPE); - } - - template<> - inline void tie_setting(value& v, QLabel* lb) - { - lb->setText(v); - base_value::connect(&v, SIGNAL(valueChanged(QString)), lb, SLOT(setText(QString)), v.SAFE_CONNTYPE); - } - - template<> - inline void tie_setting(value& v, QTabWidget* t) - { - t->setCurrentIndex(v); - base_value::connect(t, SIGNAL(currentChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); - base_value::connect(&v, SIGNAL(valueChanged(int)), t, SLOT(setCurrentIndex(int)), v.SAFE_CONNTYPE); - } -} -- cgit v1.2.3 From a1ebe07fb572781ab7a9a7da07f6f6c93a6b7833 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 1 Nov 2015 12:52:54 +0100 Subject: options: whitespace --- opentrack-compat/options.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'opentrack-compat') diff --git a/opentrack-compat/options.hpp b/opentrack-compat/options.hpp index be8c688c..9b03c18f 100644 --- a/opentrack-compat/options.hpp +++ b/opentrack-compat/options.hpp @@ -51,8 +51,7 @@ template using mem = std::shared_ptr; #define OPENTRACK_ORG "opentrack-2.3" namespace options { - template - using map = std::map; + template using map = std::map; using std::string; template @@ -405,7 +404,7 @@ namespace options { b->reload(); } }; - + template inline void tie_setting(value&, q*); -- cgit v1.2.3 From 075d7eee2c5c218451246d0407617ea2bf099274 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 1 Nov 2015 14:06:10 +0100 Subject: shm: allow everyone to use the mapping on win32 Issue: #261 --- opentrack-compat/shm.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 2 deletions(-) (limited to 'opentrack-compat') diff --git a/opentrack-compat/shm.cpp b/opentrack-compat/shm.cpp index 029a4c95..9eb1b90e 100644 --- a/opentrack-compat/shm.cpp +++ b/opentrack-compat/shm.cpp @@ -6,15 +6,115 @@ */ #include +#include #include "shm.h" #if defined(_WIN32) + +#include +#include + +struct secattr +{ + bool success; + SECURITY_DESCRIPTOR* pSD; + SECURITY_ATTRIBUTES attrs; + PSID pEveryoneSID; + PACL pACL; + + void cleanup() + { + if (pEveryoneSID) + FreeSid(pEveryoneSID); + if (pACL) + LocalFree(pACL); + if (pSD) + LocalFree(pSD); + success = false; + pSD = nullptr; + pEveryoneSID = nullptr; + pACL = nullptr; + } + + secattr() : success(true), pSD(nullptr), pEveryoneSID(nullptr), pACL(nullptr) + { + SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY; + EXPLICIT_ACCESS ea; + + if(!AllocateAndInitializeSid(&SIDAuthWorld, 1, + SECURITY_WORLD_RID, + 0, 0, 0, 0, 0, 0, 0, + &pEveryoneSID)) + { + fprintf(stderr, "AllocateAndInitializeSid: %d\n", (int) GetLastError()); + goto cleanup; + } + + memset(&ea, 0, sizeof(ea)); + + ea.grfAccessPermissions = KEY_READ; + ea.grfAccessMode = SET_ACCESS; + ea.grfInheritance = NO_INHERITANCE; + ea.Trustee.TrusteeForm = TRUSTEE_IS_SID; + ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; + ea.Trustee.ptstrName = (LPTSTR) pEveryoneSID; + + if (SetEntriesInAcl(1, &ea, NULL, &pACL) != ERROR_SUCCESS) + { + fprintf(stderr, "SetEntriesInAcl: %d\n", (int) GetLastError()); + goto cleanup; + } + + pSD = (SECURITY_DESCRIPTOR*) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH); + if (pSD == nullptr) + { + fprintf(stderr, "LocalAlloc: %d\n", (int) GetLastError()); + goto cleanup; + } + + if (!InitializeSecurityDescriptor(pSD, + SECURITY_DESCRIPTOR_REVISION)) + { + fprintf(stderr, "InitializeSecurityDescriptor: %d\n", (int) GetLastError()); + goto cleanup; + } + + if (!SetSecurityDescriptorDacl(pSD, + TRUE, + pACL, + FALSE)) + { + fprintf(stderr, "SetSecurityDescriptorDacl: %d\n", (int) GetLastError()); + goto cleanup; + } + + attrs.bInheritHandle = false; + attrs.lpSecurityDescriptor = pSD; + attrs.nLength = sizeof(SECURITY_ATTRIBUTES); + + fprintf(stderr, "security descriptor ok\n"); + fflush(stderr); + + return; +cleanup: + fflush(stderr); + cleanup(); + } + + ~secattr() + { + cleanup(); + } +}; + PortableLockedShm::PortableLockedShm(const char* shmName, const char* mutexName, int mapSize) { - hMutex = CreateMutexA(NULL, false, mutexName); + secattr sa; + + hMutex = CreateMutexA(sa.success ? &sa.attrs : nullptr, false, mutexName); hMapFile = CreateFileMappingA( INVALID_HANDLE_VALUE, - NULL, + sa.success ? &sa.attrs : nullptr, PAGE_READWRITE, 0, mapSize, -- cgit v1.2.3 From fd54d69ff9b1667e22bd3964de2bd06a2ac90cb3 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 1 Nov 2015 17:00:33 +0100 Subject: shm: fix winegcc on osx targetting 10.8 see: [ 74%] Generating opentrack-wrapper-wine.exe.so In file included from /Users/sthalik/dev/opentrack/proto-wine/opentrack-wrapper-wine-windows.cxx:7: In file included from /Users/sthalik/dev/opentrack/opentrack-compat/shm.cpp:9: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cstdio:108:10: fatal error: 'support/win32/support.h' file not found ^ 1 error generated. winegcc: clang++ failed --- opentrack-compat/shm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'opentrack-compat') diff --git a/opentrack-compat/shm.cpp b/opentrack-compat/shm.cpp index 9eb1b90e..a26b808b 100644 --- a/opentrack-compat/shm.cpp +++ b/opentrack-compat/shm.cpp @@ -6,7 +6,7 @@ */ #include -#include +#include #include "shm.h" #if defined(_WIN32) -- cgit v1.2.3 From 2cf3acb8fad9276e9fda412fd011d6c41d6195e6 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 2 Nov 2015 09:04:09 +0100 Subject: shm: guard pragma against non-GNUC --- opentrack-compat/shm.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'opentrack-compat') diff --git a/opentrack-compat/shm.h b/opentrack-compat/shm.h index c62976bd..3edb0080 100644 --- a/opentrack-compat/shm.h +++ b/opentrack-compat/shm.h @@ -19,8 +19,10 @@ #include #endif -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wattributes" +#ifdef __GNUC__ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wattributes" +#endif #ifdef BUILD_compat # include "compat-export.hpp" @@ -45,4 +47,6 @@ private: #endif }; -#pragma GCC diagnostic pop +#ifdef __GNUC__ +# pragma GCC diagnostic pop +#endif -- cgit v1.2.3 From ae71a80a7b84899fd9d8c64265a7509e71f2c380 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 5 Nov 2015 06:02:54 +0100 Subject: shm: set right permissions for the mapping Fix a copy-paste error. --- opentrack-compat/shm.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'opentrack-compat') diff --git a/opentrack-compat/shm.cpp b/opentrack-compat/shm.cpp index a26b808b..fffb3709 100644 --- a/opentrack-compat/shm.cpp +++ b/opentrack-compat/shm.cpp @@ -36,7 +36,7 @@ struct secattr pACL = nullptr; } - secattr() : success(true), pSD(nullptr), pEveryoneSID(nullptr), pACL(nullptr) + secattr(DWORD perms) : success(true), pSD(nullptr), pEveryoneSID(nullptr), pACL(nullptr) { SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY; EXPLICIT_ACCESS ea; @@ -52,7 +52,7 @@ struct secattr memset(&ea, 0, sizeof(ea)); - ea.grfAccessPermissions = KEY_READ; + ea.grfAccessPermissions = perms; ea.grfAccessMode = SET_ACCESS; ea.grfInheritance = NO_INHERITANCE; ea.Trustee.TrusteeForm = TRUSTEE_IS_SID; @@ -109,9 +109,14 @@ cleanup: PortableLockedShm::PortableLockedShm(const char* shmName, const char* mutexName, int mapSize) { - secattr sa; + secattr sa(GENERIC_ALL|SYNCHRONIZE); hMutex = CreateMutexA(sa.success ? &sa.attrs : nullptr, false, mutexName); + if (!hMutex) + { + fprintf(stderr, "CreateMutexA: %d\n", (int) GetLastError()); + fflush(stderr); + } hMapFile = CreateFileMappingA( INVALID_HANDLE_VALUE, sa.success ? &sa.attrs : nullptr, @@ -119,11 +124,21 @@ PortableLockedShm::PortableLockedShm(const char* shmName, const char* mutexName, 0, mapSize, shmName); + if (!hMapFile) + { + fprintf(stderr, "CreateFileMappingA: %d\n", (int) GetLastError()); + fflush(stderr); + } mem = MapViewOfFile(hMapFile, FILE_MAP_WRITE, 0, 0, mapSize); + if (!mem) + { + fprintf(stderr, "MapViewOfFile: %d\n", (int) GetLastError()); + fflush(stderr); + } } PortableLockedShm::~PortableLockedShm() -- cgit v1.2.3 From c4c3e746f030e6c5e5a1f8b88762fae95d1b9225 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 11 Nov 2015 06:14:13 +0100 Subject: options: konst korrektness --- opentrack-compat/options.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'opentrack-compat') diff --git a/opentrack-compat/options.hpp b/opentrack-compat/options.hpp index 9b03c18f..3e4fb20e 100644 --- a/opentrack-compat/options.hpp +++ b/opentrack-compat/options.hpp @@ -382,7 +382,7 @@ namespace options { value(pbundle b, const QString& name, t def) : value(b, string_from_qstring(name), def) {} value(pbundle b, const char* name, t def) : value(b, string(name), def) {} - operator t() + operator t() const { return b->contains(self_name) ? b->get(self_name) : def; } -- cgit v1.2.3 From e44b2853196565ebd22eb1e27109e5e54e77ed15 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 22 Nov 2015 15:13:47 +0100 Subject: compat/options: no need to heap-alloc here --- opentrack-compat/options.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'opentrack-compat') diff --git a/opentrack-compat/options.cpp b/opentrack-compat/options.cpp index 91cd3664..48cc514b 100644 --- a/opentrack-compat/options.cpp +++ b/opentrack-compat/options.cpp @@ -7,8 +7,8 @@ namespace detail { OPENTRACK_COMPAT_EXPORT opt_singleton& singleton() { - static auto ret = std::make_shared(); - return *ret; + static opt_singleton ret; + return ret; } } -- cgit v1.2.3 From 22d4c7cc0cadf212052ef72c3bf8ddda6f73a647 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 3 Dec 2015 22:31:57 +0100 Subject: api, compat: clean up verbose fprintf logspam --- opentrack-compat/shm.cpp | 1 - opentrack/keybinding-worker.cpp | 1 - opentrack/win32-joystick.cpp | 1 - 3 files changed, 3 deletions(-) (limited to 'opentrack-compat') diff --git a/opentrack-compat/shm.cpp b/opentrack-compat/shm.cpp index fffb3709..a306d581 100644 --- a/opentrack-compat/shm.cpp +++ b/opentrack-compat/shm.cpp @@ -92,7 +92,6 @@ struct secattr attrs.lpSecurityDescriptor = pSD; attrs.nLength = sizeof(SECURITY_ATTRIBUTES); - fprintf(stderr, "security descriptor ok\n"); fflush(stderr); return; diff --git a/opentrack/keybinding-worker.cpp b/opentrack/keybinding-worker.cpp index 59bc4afd..3c801cf2 100644 --- a/opentrack/keybinding-worker.cpp +++ b/opentrack/keybinding-worker.cpp @@ -24,7 +24,6 @@ bool Key::should_process() } KeybindingWorker::~KeybindingWorker() { - qDebug() << "keybinding worker stop"; should_quit = true; wait(); if (dinkeyboard) { diff --git a/opentrack/win32-joystick.cpp b/opentrack/win32-joystick.cpp index 0f8a08f8..cd3502b4 100644 --- a/opentrack/win32-joystick.cpp +++ b/opentrack/win32-joystick.cpp @@ -122,7 +122,6 @@ win32_joy_ctx::win32_joy_ctx() void win32_joy_ctx::release() { - qDebug() << "release joystick dinput handle"; joys() = std::unordered_map>(); { auto& di = dinput_handle(); -- cgit v1.2.3 From 35e959cf6836522d1d0e4506a9756470c9e904ee Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 3 Dec 2015 22:32:15 +0100 Subject: compat/shm: move header inclusion after guard --- opentrack-compat/shm.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'opentrack-compat') diff --git a/opentrack-compat/shm.cpp b/opentrack-compat/shm.cpp index a306d581..19199bd9 100644 --- a/opentrack-compat/shm.cpp +++ b/opentrack-compat/shm.cpp @@ -5,12 +5,12 @@ * copyright notice and this permission notice appear in all copies. */ +#if defined(_WIN32) + #include #include #include "shm.h" -#if defined(_WIN32) - #include #include -- cgit v1.2.3 From 179870eb019f5b2d2327292e8beef63c01a33e4a Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 6 Dec 2015 00:51:40 +0100 Subject: compat/shm: fix build --- opentrack-compat/shm.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'opentrack-compat') diff --git a/opentrack-compat/shm.cpp b/opentrack-compat/shm.cpp index 19199bd9..55aa7a41 100644 --- a/opentrack-compat/shm.cpp +++ b/opentrack-compat/shm.cpp @@ -5,11 +5,12 @@ * copyright notice and this permission notice appear in all copies. */ +#include "shm.h" + #if defined(_WIN32) #include #include -#include "shm.h" #include #include -- cgit v1.2.3 From 211a17996e3ffc0d84c985c43c612da32923b42d Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 6 Dec 2015 02:24:44 +0100 Subject: api/camera-names: move to compat/ --- opentrack-compat/CMakeLists.txt | 1 + opentrack-compat/camera-names.cpp | 113 ++++++++++++++++++++++++++++++++ opentrack-compat/camera-names.hpp | 22 +++++++ opentrack/CMakeLists.txt | 1 - opentrack/camera-names.cpp | 113 -------------------------------- opentrack/camera-names.hpp | 16 ----- opentrack/opencv-camera-dialog.hpp | 2 +- tracker-aruco/ftnoir_tracker_aruco.cpp | 2 +- tracker-pt/ftnoir_tracker_pt.cpp | 2 +- tracker-pt/ftnoir_tracker_pt_dialog.cpp | 2 +- 10 files changed, 140 insertions(+), 134 deletions(-) create mode 100644 opentrack-compat/camera-names.cpp create mode 100644 opentrack-compat/camera-names.hpp delete mode 100644 opentrack/camera-names.cpp delete mode 100644 opentrack/camera-names.hpp (limited to 'opentrack-compat') diff --git a/opentrack-compat/CMakeLists.txt b/opentrack-compat/CMakeLists.txt index 37d53e6c..8dba67e2 100644 --- a/opentrack-compat/CMakeLists.txt +++ b/opentrack-compat/CMakeLists.txt @@ -2,3 +2,4 @@ opentrack_boilerplate(opentrack-compat NO-COMPAT NO-LINKER-SCRIPT LINKAGE) if(NOT WIN32 AND NOT APPLE) target_link_libraries(opentrack-compat rt) endif() +link_with_dinput8(opentrack-compat) diff --git a/opentrack-compat/camera-names.cpp b/opentrack-compat/camera-names.cpp new file mode 100644 index 00000000..72bcf41a --- /dev/null +++ b/opentrack-compat/camera-names.cpp @@ -0,0 +1,113 @@ +#include "camera-names.hpp" + +#if defined(OPENTRACK_API) && defined(_WIN32) +# define NO_DSHOW_STRSAFE +# include +# include +#endif + +#if defined(OPENTRACK_API) && (defined(__unix) || defined(__linux) || defined(__APPLE__)) +# include +#endif + +#ifdef __linux +#include +#include +#include +#include +#endif + +#include + +OPENTRACK_COMPAT_EXPORT int camera_name_to_index(const QString &name) +{ + auto list = get_camera_names(); + int ret = list.indexOf(name); + if (ret < 0) + ret = 0; + return ret; +} + +OPENTRACK_COMPAT_EXPORT QList get_camera_names() { + QList ret; +#if defined(_WIN32) + // Create the System Device Enumerator. + HRESULT hr; + hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); + if (FAILED(hr)) + qDebug() << "failed CoInitializeEx" << hr; + ICreateDevEnum *pSysDevEnum = NULL; + hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void **)&pSysDevEnum); + if (FAILED(hr)) + { + qDebug() << "failed CLSID_SystemDeviceEnum" << hr; + return ret; + } + // Obtain a class enumerator for the video compressor category. + IEnumMoniker *pEnumCat = NULL; + hr = pSysDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnumCat, 0); + + if (hr == S_OK) { + // Enumerate the monikers. + IMoniker *pMoniker = NULL; + ULONG cFetched; + while (pEnumCat->Next(1, &pMoniker, &cFetched) == S_OK) { + IPropertyBag *pPropBag; + hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pPropBag); + if (SUCCEEDED(hr)) { + // To retrieve the filter's friendly name, do the following: + VARIANT varName; + VariantInit(&varName); + hr = pPropBag->Read(L"FriendlyName", &varName, 0); + if (SUCCEEDED(hr)) + { + // Display the name in your UI somehow. + QString str((QChar*)varName.bstrVal, wcslen(varName.bstrVal)); + ret.append(str); + } + VariantClear(&varName); + + ////// To create an instance of the filter, do the following: + ////IBaseFilter *pFilter; + ////hr = pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter, + //// (void**)&pFilter); + // Now add the filter to the graph. + //Remember to release pFilter later. + pPropBag->Release(); + } + pMoniker->Release(); + } + pEnumCat->Release(); + } + else + qDebug() << "failed CLSID_VideoInputDeviceCategory" << hr; + + pSysDevEnum->Release(); +#endif +#ifdef __linux + for (int i = 0; i < 16; i++) { + char buf[128]; + sprintf(buf, "/dev/video%d", i); + if (access(buf, F_OK) == 0) + ret.append(buf); + else + continue; + + if (access(buf, R_OK | W_OK) == 0) { + int fd = open(buf, O_RDONLY); + if (fd == -1) + continue; + struct v4l2_capability video_cap; + if(ioctl(fd, VIDIOC_QUERYCAP, &video_cap) == -1) + { + qDebug() << "VIDIOC_QUERYCAP" << errno; + close(fd); + continue; + } + ret[ret.size()-1] = reinterpret_cast(video_cap.card); + close(fd); + } + } +#endif + return ret; +} \ No newline at end of file diff --git a/opentrack-compat/camera-names.hpp b/opentrack-compat/camera-names.hpp new file mode 100644 index 00000000..c2879000 --- /dev/null +++ b/opentrack-compat/camera-names.hpp @@ -0,0 +1,22 @@ +/* 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 +#include + +#ifdef BUILD_compat +# include "compat-export.hpp" +#else +# include "compat-import.hpp" +#endif + +OPENTRACK_COMPAT_EXPORT QList get_camera_names(); +OPENTRACK_COMPAT_EXPORT int camera_name_to_index(const QString &name); + diff --git a/opentrack/CMakeLists.txt b/opentrack/CMakeLists.txt index de5975e2..08003a5c 100644 --- a/opentrack/CMakeLists.txt +++ b/opentrack/CMakeLists.txt @@ -4,5 +4,4 @@ if(NOT WIN32) else() target_link_libraries(opentrack-api winmm) endif() -link_with_dinput8(opentrack-api) target_link_libraries(opentrack-api opentrack-spline-widget) diff --git a/opentrack/camera-names.cpp b/opentrack/camera-names.cpp deleted file mode 100644 index 74e998ca..00000000 --- a/opentrack/camera-names.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include "camera-names.hpp" - -#if defined(OPENTRACK_API) && defined(_WIN32) -# define NO_DSHOW_STRSAFE -# include -# include -#endif - -#if defined(OPENTRACK_API) && (defined(__unix) || defined(__linux) || defined(__APPLE__)) -# include -#endif - -#ifdef __linux -#include -#include -#include -#include -#endif - -#include - -int camera_name_to_index(const QString &name) -{ - auto list = get_camera_names(); - int ret = list.indexOf(name); - if (ret < 0) - ret = 0; - return ret; -} - -QList get_camera_names() { - QList ret; -#if defined(_WIN32) - // Create the System Device Enumerator. - HRESULT hr; - hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); - if (FAILED(hr)) - qDebug() << "failed CoInitializeEx" << hr; - ICreateDevEnum *pSysDevEnum = NULL; - hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void **)&pSysDevEnum); - if (FAILED(hr)) - { - qDebug() << "failed CLSID_SystemDeviceEnum" << hr; - return ret; - } - // Obtain a class enumerator for the video compressor category. - IEnumMoniker *pEnumCat = NULL; - hr = pSysDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnumCat, 0); - - if (hr == S_OK) { - // Enumerate the monikers. - IMoniker *pMoniker = NULL; - ULONG cFetched; - while (pEnumCat->Next(1, &pMoniker, &cFetched) == S_OK) { - IPropertyBag *pPropBag; - hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pPropBag); - if (SUCCEEDED(hr)) { - // To retrieve the filter's friendly name, do the following: - VARIANT varName; - VariantInit(&varName); - hr = pPropBag->Read(L"FriendlyName", &varName, 0); - if (SUCCEEDED(hr)) - { - // Display the name in your UI somehow. - QString str((QChar*)varName.bstrVal, wcslen(varName.bstrVal)); - ret.append(str); - } - VariantClear(&varName); - - ////// To create an instance of the filter, do the following: - ////IBaseFilter *pFilter; - ////hr = pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter, - //// (void**)&pFilter); - // Now add the filter to the graph. - //Remember to release pFilter later. - pPropBag->Release(); - } - pMoniker->Release(); - } - pEnumCat->Release(); - } - else - qDebug() << "failed CLSID_VideoInputDeviceCategory" << hr; - - pSysDevEnum->Release(); -#endif -#ifdef __linux - for (int i = 0; i < 16; i++) { - char buf[128]; - sprintf(buf, "/dev/video%d", i); - if (access(buf, F_OK) == 0) - ret.append(buf); - else - continue; - - if (access(buf, R_OK | W_OK) == 0) { - int fd = open(buf, O_RDONLY); - if (fd == -1) - continue; - struct v4l2_capability video_cap; - if(ioctl(fd, VIDIOC_QUERYCAP, &video_cap) == -1) - { - qDebug() << "VIDIOC_QUERYCAP" << errno; - close(fd); - continue; - } - ret[ret.size()-1] = reinterpret_cast(video_cap.card); - close(fd); - } - } -#endif - return ret; -} \ No newline at end of file diff --git a/opentrack/camera-names.hpp b/opentrack/camera-names.hpp deleted file mode 100644 index ef914458..00000000 --- a/opentrack/camera-names.hpp +++ /dev/null @@ -1,16 +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 -#include - -QList get_camera_names(); -int camera_name_to_index(const QString &name); - diff --git a/opentrack/opencv-camera-dialog.hpp b/opentrack/opencv-camera-dialog.hpp index 0d4a51af..96c7a643 100644 --- a/opentrack/opencv-camera-dialog.hpp +++ b/opentrack/opencv-camera-dialog.hpp @@ -12,7 +12,7 @@ #include #include #include -#include "opentrack/camera-names.hpp" +#include "opentrack-compat/camera-names.hpp" #ifdef __linux #include diff --git a/tracker-aruco/ftnoir_tracker_aruco.cpp b/tracker-aruco/ftnoir_tracker_aruco.cpp index 1da5b8df..316c7e13 100644 --- a/tracker-aruco/ftnoir_tracker_aruco.cpp +++ b/tracker-aruco/ftnoir_tracker_aruco.cpp @@ -16,7 +16,7 @@ #include #include #include -#include "opentrack/camera-names.hpp" +#include "opentrack-compat/camera-names.hpp" #include "opentrack-compat/sleep.hpp" typedef struct { diff --git a/tracker-pt/ftnoir_tracker_pt.cpp b/tracker-pt/ftnoir_tracker_pt.cpp index 2dbf9068..3dd91a45 100644 --- a/tracker-pt/ftnoir_tracker_pt.cpp +++ b/tracker-pt/ftnoir_tracker_pt.cpp @@ -12,7 +12,7 @@ #include #include #include -#include "opentrack/camera-names.hpp" +#include "opentrack-compat/camera-names.hpp" #include "opentrack-compat/sleep.hpp" //#define PT_PERF_LOG //log performance diff --git a/tracker-pt/ftnoir_tracker_pt_dialog.cpp b/tracker-pt/ftnoir_tracker_pt_dialog.cpp index b1ae2238..551910f7 100644 --- a/tracker-pt/ftnoir_tracker_pt_dialog.cpp +++ b/tracker-pt/ftnoir_tracker_pt_dialog.cpp @@ -12,7 +12,7 @@ #include #include #include -#include "opentrack/camera-names.hpp" +#include "opentrack-compat/camera-names.hpp" #include //----------------------------------------------------------------------------- -- cgit v1.2.3 From 69a188510e14130e2aded0c9398a67d4e3ff4dcd Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 6 Dec 2015 19:53:06 +0100 Subject: compat/options: move from header --- opentrack-compat/options.cpp | 190 +++++++++++++++++++++++++++++++++++++++ opentrack-compat/options.hpp | 206 ++++++------------------------------------- 2 files changed, 218 insertions(+), 178 deletions(-) (limited to 'opentrack-compat') diff --git a/opentrack-compat/options.cpp b/opentrack-compat/options.cpp index 48cc514b..7938b075 100644 --- a/opentrack-compat/options.cpp +++ b/opentrack-compat/options.cpp @@ -13,4 +13,194 @@ OPENTRACK_COMPAT_EXPORT opt_singleton& singleton() } +group::group(const string& name) : name(name) +{ + auto conf = ini_file(); + auto q_name = QString::fromStdString(name); + conf->beginGroup(q_name); + for (auto& k_ : conf->childKeys()) + { + auto tmp = k_.toUtf8(); + string k(tmp); + kvs[k] = conf->value(k_); + } + conf->endGroup(); +} + +void group::save() +{ + auto s = ini_file(); + auto q_name = QString::fromStdString(name); + s->beginGroup(q_name); + for (auto& i : kvs) + { + auto k = QString::fromStdString(i.first); + s->setValue(k, i.second); + } + s->endGroup(); + s->sync(); +} + +void group::put(const std::string &s, const QVariant &d) +{ + kvs[s] = d; +} + +bool group::contains(const std::string &s) +{ + return kvs.count(s) != 0; +} + +QString group::ini_directory() +{ + const auto dirs = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); + if (dirs.size() == 0) + return ""; + if (QDir(dirs[0]).mkpath(OPENTRACK_ORG)) + return dirs[0] + "/" OPENTRACK_ORG; + return ""; +} + +QString group::ini_filename() +{ + QSettings settings(OPENTRACK_ORG); + return settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); +} + +QString group::ini_pathname() +{ + const auto dir = ini_directory(); + if (dir == "") + return ""; + QSettings settings(OPENTRACK_ORG); + return dir + "/" + settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); +} + +const QStringList group::ini_list() +{ + const auto dirname = ini_directory(); + if (dirname == "") + return QStringList(); + QDir settings_dir(dirname); + return settings_dir.entryList( QStringList { "*.ini" } , QDir::Files, QDir::Name ); +} + +const mem group::ini_file() +{ + const auto pathname = ini_pathname(); + if (pathname != "") + return std::make_shared(ini_pathname(), QSettings::IniFormat); + return std::make_shared(); +} + +impl_bundle::impl_bundle(const std::string &group_name) + : + mtx(QMutex::Recursive), + group_name(group_name), + saved(group_name), + transient(saved), + modified(false) +{} + +void impl_bundle::reload() +{ + { + QMutexLocker l(&mtx); + saved = group(group_name); + transient = saved; + modified = false; + } + emit reloading(); +} + +void impl_bundle::store_kv(const std::string &name, const QVariant &datum) +{ + QMutexLocker l(&mtx); + + auto old = transient.get(name); + if (!transient.contains(name) || datum != old) + { + modified = true; + transient.put(name, datum); + } +} + +bool impl_bundle::contains(const std::string &name) +{ + QMutexLocker l(&mtx); + return transient.contains(name); +} + +void impl_bundle::save() +{ + { + QMutexLocker l(&mtx); + modified = false; + saved = transient; + transient.save(); + } + emit saving(); +} + +bool impl_bundle::modifiedp() +{ + QMutexLocker l(&mtx); + return modified; +} + +namespace detail +{ + +pbundle opt_singleton::bundle(const opt_singleton::k &key) +{ + QMutexLocker l(&implsgl_mtx); + + if (implsgl_data.count(key) != 0) + { + auto shared = std::get<1>(implsgl_data[key]).lock(); + if (shared != nullptr) + return shared; + } + + qDebug() << "bundle +" << QString::fromStdString(key); + + auto shr = std::make_shared(key); + implsgl_data[key] = tt(cnt(1), shr); + return shr; +} + +void opt_singleton::bundle_decf(const opt_singleton::k &key) +{ + QMutexLocker l(&implsgl_mtx); + + if (--std::get<0>(implsgl_data[key]) == 0) + implsgl_data.erase(key); +} + +opt_singleton::opt_singleton() : implsgl_mtx(QMutex::Recursive) {} + +} + +opt_bundle::opt_bundle(const std::string &group_name) + : impl_bundle(group_name) +{ +} + +opt_bundle::~opt_bundle() +{ + qDebug() << "bundle -" << QString::fromStdString(group_name); + detail::singleton().bundle_decf(group_name); +} + +base_value::base_value(pbundle b, const std::string &name) : b(b), self_name(name) {} + +opts::~opts() +{ + b->reload(); +} + +opts::opts(const std::string &name) : b(bundle(name)) {} + + + } diff --git a/opentrack-compat/options.hpp b/opentrack-compat/options.hpp index 3e4fb20e..81a53067 100644 --- a/opentrack-compat/options.hpp +++ b/opentrack-compat/options.hpp @@ -90,96 +90,26 @@ namespace options { } // snapshot of qsettings group at given time - class group { + class OPENTRACK_COMPAT_EXPORT group { private: map kvs; string name; public: - group(const string& name) : name(name) - { - auto conf = ini_file(); - auto q_name = QString::fromStdString(name); - conf->beginGroup(q_name); - for (auto& k_ : conf->childKeys()) - { - auto tmp = k_.toUtf8(); - string k(tmp); - kvs[k] = conf->value(k_); - } - conf->endGroup(); - } - - void save() - { - auto s = ini_file(); - auto q_name = QString::fromStdString(name); - s->beginGroup(q_name); - for (auto& i : kvs) - { - auto k = QString::fromStdString(i.first); - s->setValue(k, i.second); - } - s->endGroup(); - s->sync(); - } + group(const string& name); + void save(); + void put(const string& s, const QVariant& d); + bool contains(const string& s); + static QString ini_directory(); + static QString ini_filename(); + static QString ini_pathname(); + static const QStringList ini_list(); + static const mem ini_file(); template t get(const string& k) { return qcruft_to_t(kvs[k]); } - - void put(const string& s, const QVariant& d) - { - kvs[s] = d; - } - - bool contains(const string& s) - { - return kvs.count(s) != 0; - } - - static QString ini_directory() - { - const auto dirs = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); - if (dirs.size() == 0) - return ""; - if (QDir(dirs[0]).mkpath(OPENTRACK_ORG)) - return dirs[0] + "/" OPENTRACK_ORG; - return ""; - } - - static QString ini_filename() - { - QSettings settings(OPENTRACK_ORG); - return settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); - } - - static QString ini_pathname() - { - const auto dir = ini_directory(); - if (dir == "") - return ""; - QSettings settings(OPENTRACK_ORG); - return dir + "/" + settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); - } - - static const QStringList ini_list() - { - const auto dirname = ini_directory(); - if (dirname == "") - return QStringList(); - QDir settings_dir(dirname); - return settings_dir.entryList( QStringList { "*.ini" } , QDir::Files, QDir::Name ); - } - - static const mem ini_file() - { - const auto pathname = ini_pathname(); - if (pathname != "") - return std::make_shared(ini_pathname(), QSettings::IniFormat); - return std::make_shared(); - } }; class OPENTRACK_COMPAT_EXPORT impl_bundle : public QObject { @@ -196,64 +126,20 @@ namespace options { void reloading(); void saving(); public: - impl_bundle(const string& group_name) : - mtx(QMutex::Recursive), - group_name(group_name), - saved(group_name), - transient(saved), - modified(false) - { - } - + impl_bundle(const string& group_name); string name() { return group_name; } - - void reload() { - { - QMutexLocker l(&mtx); - saved = group(group_name); - transient = saved; - modified = false; - } - emit reloading(); - } - - void store_kv(const string& name, const QVariant& datum) - { - QMutexLocker l(&mtx); - - auto old = transient.get(name); - if (!transient.contains(name) || datum != old) - { - modified = true; - transient.put(name, datum); - } - } - bool contains(const string& name) - { - QMutexLocker l(&mtx); - return transient.contains(name); - } + void reload(); + void store_kv(const string& name, const QVariant& datum); + bool contains(const string& name); + void save(); + bool modifiedp(); + template t get(const string& name) { QMutexLocker l(&mtx); return transient.get(name); } - void save() - { - { - QMutexLocker l(&mtx); - modified = false; - saved = transient; - transient.save(); - } - emit saving(); - } - - bool modifiedp() { - QMutexLocker l(&mtx); - return modified; - } }; class opt_bundle; @@ -272,33 +158,9 @@ namespace options { QMutex implsgl_mtx; map implsgl_data; public: - opt_singleton() : implsgl_mtx(QMutex::Recursive) {} - - pbundle bundle(const k& key) - { - QMutexLocker l(&implsgl_mtx); - - if (implsgl_data.count(key) != 0) - { - auto shared = std::get<1>(implsgl_data[key]).lock(); - if (shared != nullptr) - return shared; - } - - qDebug() << "bundle +" << QString::fromStdString(key); - - auto shr = std::make_shared(key); - implsgl_data[key] = tt(cnt(1), shr); - return shr; - } - - void bundle_decf(const k& key) - { - QMutexLocker l(&implsgl_mtx); - - if (--std::get<0>(implsgl_data[key]) == 0) - implsgl_data.erase(key); - } + opt_singleton(); + pbundle bundle(const k& key); + void bundle_decf(const k& key); }; OPENTRACK_COMPAT_EXPORT opt_singleton& singleton(); @@ -308,19 +170,12 @@ namespace options { static inline pbundle bundle(const string name) { return detail::singleton().bundle(name); } - class opt_bundle : public impl_bundle + class OPENTRACK_COMPAT_EXPORT opt_bundle : public impl_bundle { public: opt_bundle() : impl_bundle("i-have-no-name") {} - opt_bundle(const string& group_name) : impl_bundle(group_name) - { - } - - ~opt_bundle() - { - qDebug() << "bundle -" << QString::fromStdString(group_name); - detail::singleton().bundle_decf(group_name); - } + opt_bundle(const string& group_name); + ~opt_bundle(); }; class OPENTRACK_COMPAT_EXPORT base_value : public QObject @@ -330,7 +185,7 @@ namespace options { #define DEFINE_SIGNAL(t) void valueChanged(t) public: string name() { return self_name; } - base_value(pbundle b, const string& name) : b(b), self_name(name) {} + base_value(pbundle b, const string& name); signals: DEFINE_SIGNAL(double); DEFINE_SIGNAL(int); @@ -355,7 +210,7 @@ namespace options { virtual void reload() = 0; }; - static inline string string_from_qstring(const QString& datum) + static inline std::string string_from_qstring(const QString &datum) { auto tmp = datum.toUtf8(); return string(tmp.constData()); @@ -393,16 +248,11 @@ namespace options { t def; }; - struct opts + struct OPENTRACK_COMPAT_EXPORT opts { pbundle b; - - opts(const std::string& name) : b(bundle(name)) {} - - ~opts() - { - b->reload(); - } + opts(const std::string& name); + ~opts(); }; template -- cgit v1.2.3 From dcd4df5e49e6207273c961069d14ce42ad83d147 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 6 Dec 2015 20:02:18 +0100 Subject: compat/options: get rid of std::string usage It's pointless to use it here. --- opentrack-compat/options.cpp | 37 +++++++++++++------------------- opentrack-compat/options.hpp | 51 ++++++++++++++++++-------------------------- 2 files changed, 36 insertions(+), 52 deletions(-) (limited to 'opentrack-compat') diff --git a/opentrack-compat/options.cpp b/opentrack-compat/options.cpp index 7938b075..550fec24 100644 --- a/opentrack-compat/options.cpp +++ b/opentrack-compat/options.cpp @@ -13,15 +13,14 @@ OPENTRACK_COMPAT_EXPORT opt_singleton& singleton() } -group::group(const string& name) : name(name) +group::group(const QString& name) : name(name) { auto conf = ini_file(); - auto q_name = QString::fromStdString(name); - conf->beginGroup(q_name); + conf->beginGroup(name); for (auto& k_ : conf->childKeys()) { auto tmp = k_.toUtf8(); - string k(tmp); + QString k(tmp); kvs[k] = conf->value(k_); } conf->endGroup(); @@ -30,23 +29,19 @@ group::group(const string& name) : name(name) void group::save() { auto s = ini_file(); - auto q_name = QString::fromStdString(name); - s->beginGroup(q_name); + s->beginGroup(name); for (auto& i : kvs) - { - auto k = QString::fromStdString(i.first); - s->setValue(k, i.second); - } + s->setValue(i.first, i.second); s->endGroup(); s->sync(); } -void group::put(const std::string &s, const QVariant &d) +void group::put(const QString &s, const QVariant &d) { kvs[s] = d; } -bool group::contains(const std::string &s) +bool group::contains(const QString &s) { return kvs.count(s) != 0; } @@ -93,7 +88,7 @@ const mem group::ini_file() return std::make_shared(); } -impl_bundle::impl_bundle(const std::string &group_name) +impl_bundle::impl_bundle(const QString &group_name) : mtx(QMutex::Recursive), group_name(group_name), @@ -113,7 +108,7 @@ void impl_bundle::reload() emit reloading(); } -void impl_bundle::store_kv(const std::string &name, const QVariant &datum) +void impl_bundle::store_kv(const QString &name, const QVariant &datum) { QMutexLocker l(&mtx); @@ -125,7 +120,7 @@ void impl_bundle::store_kv(const std::string &name, const QVariant &datum) } } -bool impl_bundle::contains(const std::string &name) +bool impl_bundle::contains(const QString &name) { QMutexLocker l(&mtx); return transient.contains(name); @@ -162,7 +157,7 @@ pbundle opt_singleton::bundle(const opt_singleton::k &key) return shared; } - qDebug() << "bundle +" << QString::fromStdString(key); + qDebug() << "bundle +" << key; auto shr = std::make_shared(key); implsgl_data[key] = tt(cnt(1), shr); @@ -181,26 +176,24 @@ opt_singleton::opt_singleton() : implsgl_mtx(QMutex::Recursive) {} } -opt_bundle::opt_bundle(const std::string &group_name) +opt_bundle::opt_bundle(const QString &group_name) : impl_bundle(group_name) { } opt_bundle::~opt_bundle() { - qDebug() << "bundle -" << QString::fromStdString(group_name); + qDebug() << "bundle -" << group_name; detail::singleton().bundle_decf(group_name); } -base_value::base_value(pbundle b, const std::string &name) : b(b), self_name(name) {} +base_value::base_value(pbundle b, const QString &name) : b(b), self_name(name) {} opts::~opts() { b->reload(); } -opts::opts(const std::string &name) : b(bundle(name)) {} - - +opts::opts(const QString &name) : b(bundle(name)) {} } diff --git a/opentrack-compat/options.hpp b/opentrack-compat/options.hpp index 81a53067..d41c5cd1 100644 --- a/opentrack-compat/options.hpp +++ b/opentrack-compat/options.hpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -52,7 +51,6 @@ template using mem = std::shared_ptr; namespace options { template using map = std::map; - using std::string; template // don't elide usages of the function, qvariant default implicit @@ -92,13 +90,13 @@ namespace options { // snapshot of qsettings group at given time class OPENTRACK_COMPAT_EXPORT group { private: - map kvs; - string name; + map kvs; + QString name; public: - group(const string& name); + group(const QString& name); void save(); - void put(const string& s, const QVariant& d); - bool contains(const string& s); + void put(const QString& s, const QVariant& d); + bool contains(const QString& s); static QString ini_directory(); static QString ini_filename(); static QString ini_pathname(); @@ -106,7 +104,7 @@ namespace options { static const mem ini_file(); template - t get(const string& k) + t get(const QString& k) { return qcruft_to_t(kvs[k]); } @@ -116,7 +114,7 @@ namespace options { Q_OBJECT protected: QMutex mtx; - const string group_name; + const QString group_name; group saved; group transient; bool modified; @@ -126,16 +124,16 @@ namespace options { void reloading(); void saving(); public: - impl_bundle(const string& group_name); - string name() { return group_name; } + impl_bundle(const QString& group_name); + QString name() { return group_name; } void reload(); - void store_kv(const string& name, const QVariant& datum); - bool contains(const string& name); + void store_kv(const QString& name, const QVariant& datum); + bool contains(const QString& name); void save(); bool modifiedp(); template - t get(const string& name) + t get(const QString& name) { QMutexLocker l(&mtx); return transient.get(name); @@ -149,7 +147,7 @@ namespace options { struct OPENTRACK_COMPAT_EXPORT opt_singleton { public: - using k = std::string; + using k = QString; using v = opt_bundle; using cnt = int; using pbundle = std::shared_ptr; @@ -168,13 +166,13 @@ namespace options { using pbundle = std::shared_ptr; - static inline pbundle bundle(const string name) { return detail::singleton().bundle(name); } + static inline pbundle bundle(const QString name) { return detail::singleton().bundle(name); } class OPENTRACK_COMPAT_EXPORT opt_bundle : public impl_bundle { public: opt_bundle() : impl_bundle("i-have-no-name") {} - opt_bundle(const string& group_name); + opt_bundle(const QString& group_name); ~opt_bundle(); }; @@ -184,8 +182,8 @@ namespace options { #define DEFINE_SLOT(t) void setValue(t datum) { store(datum); } #define DEFINE_SIGNAL(t) void valueChanged(t) public: - string name() { return self_name; } - base_value(pbundle b, const string& name); + QString name() { return self_name; } + base_value(pbundle b, const QString& name); signals: DEFINE_SIGNAL(double); DEFINE_SIGNAL(int); @@ -193,7 +191,7 @@ namespace options { DEFINE_SIGNAL(QString); protected: pbundle b; - string self_name; + QString self_name; template void store(const t& datum) @@ -210,12 +208,6 @@ namespace options { virtual void reload() = 0; }; - static inline std::string string_from_qstring(const QString &datum) - { - auto tmp = datum.toUtf8(); - return string(tmp.constData()); - } - template class value : public base_value { public: @@ -226,7 +218,7 @@ namespace options { } static constexpr const Qt::ConnectionType DIRECT_CONNTYPE = Qt::DirectConnection; static constexpr const Qt::ConnectionType SAFE_CONNTYPE = Qt::UniqueConnection; - value(pbundle b, const string& name, t def) : base_value(b, name), def(def) + value(pbundle b, const QString& name, t def) : base_value(b, name), def(def) { QObject::connect(b.get(), SIGNAL(reloading()), this, SLOT(reload()), @@ -234,8 +226,7 @@ namespace options { if (!b->contains(name) || b->get(name).type() == QVariant::Invalid) *this = def; } - value(pbundle b, const QString& name, t def) : value(b, string_from_qstring(name), def) {} - value(pbundle b, const char* name, t def) : value(b, string(name), def) {} + value(pbundle b, const char* name, t def) : value(b, QString(name), def) {} operator t() const { @@ -251,7 +242,7 @@ namespace options { struct OPENTRACK_COMPAT_EXPORT opts { pbundle b; - opts(const std::string& name); + opts(const QString& name); ~opts(); }; -- cgit v1.2.3