summaryrefslogtreecommitdiffhomepage
path: root/opentrack
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2015-11-22 15:49:52 +0100
committerStanislaw Halik <sthalik@misaki.pl>2015-11-22 15:55:44 +0100
commit86f1df90b405b56b46d698b4059ed2c6b10df8b3 (patch)
tree94fd2ed0d5caa4e2ae0a617d598787d3e2890e3a /opentrack
parente44b2853196565ebd22eb1e27109e5e54e77ed15 (diff)
api/joy: protect dinput with mutex in all cases
Also practice object-oriented information hiding to avoid accesses without the mutex. Also provide basic joy information (guid, name) without exposing dinput handles and so on.
Diffstat (limited to 'opentrack')
-rw-r--r--opentrack/win32-joystick-shortcuts.cpp6
-rw-r--r--opentrack/win32-joystick-shortcuts.hpp132
2 files changed, 82 insertions, 56 deletions
diff --git a/opentrack/win32-joystick-shortcuts.cpp b/opentrack/win32-joystick-shortcuts.cpp
index 44659b7e..61a2a74a 100644
--- a/opentrack/win32-joystick-shortcuts.cpp
+++ b/opentrack/win32-joystick-shortcuts.cpp
@@ -22,3 +22,9 @@ std::unordered_map<QString, std::shared_ptr<win32_joy_ctx::joy>>& win32_joy_ctx:
return js;
}
+
+win32_joy_ctx& win32_joy_ctx::make()
+{
+ static win32_joy_ctx ret;
+ return ret;
+}
diff --git a/opentrack/win32-joystick-shortcuts.hpp b/opentrack/win32-joystick-shortcuts.hpp
index 081dc0ce..ed0b4412 100644
--- a/opentrack/win32-joystick-shortcuts.hpp
+++ b/opentrack/win32-joystick-shortcuts.hpp
@@ -55,13 +55,13 @@ struct OPENTRACK_EXPORT win32_joy_ctx
{
QMutexLocker l(&mtx);
+ refresh(false);
+
auto iter = joys().find(guid);
if (iter == joys().end() || iter->second->joy_handle == nullptr)
return false;
- refresh(false);
-
auto& j = iter->second;
auto& joy_handle = j->joy_handle;
@@ -107,7 +107,79 @@ struct OPENTRACK_EXPORT win32_joy_ctx
return true;
}
+
+ ~win32_joy_ctx()
+ {
+ release();
+ }
+
+ enum { joy_axis_size = 65535 };
+
+ struct joy_info
+ {
+ QString name, guid;
+ };
+
+ std::vector<joy_info> get_joy_info()
+ {
+ QMutexLocker l(&mtx);
+
+ std::vector<joy_info> ret;
+
+ for (auto& j : joys())
+ ret.push_back(joy_info { j.second->name, j.first });
+
+ return ret;
+ }
+
+ static win32_joy_ctx& make();
+
+ win32_joy_ctx(const win32_joy_ctx&) = delete;
+ win32_joy_ctx& operator=(const win32_joy_ctx&) = delete;
+private:
+ static QString guid_to_string(const GUID guid)
+ {
+ char buf[40] = {0};
+ wchar_t szGuidW[40] = {0};
+
+ StringFromGUID2(guid, szGuidW, 40);
+ WideCharToMultiByte(0, 0, szGuidW, -1, buf, 40, NULL, NULL);
+
+ return QString(buf);
+ }
+
+ static LPDIRECTINPUT8& dinput_handle();
+
+ win32_joy_ctx()
+ {
+ refresh(true);
+ }
+ void release()
+ {
+ qDebug() << "release joystick dinput handle";
+ joys() = std::unordered_map<QString, std::shared_ptr<joy>>();
+ {
+ auto& di = dinput_handle();
+ di->Release();
+ di = nullptr;
+ }
+ }
+ void refresh(bool first)
+ {
+ if (!first)
+ {
+ if (timer_joylist.elapsed_ms() < joylist_refresh_ms)
+ return;
+ timer_joylist.start();
+ }
+
+ enum_state st(joys(), first);
+ }
+ QMutex mtx;
+ Timer timer_joylist;
+ enum { joylist_refresh_ms = 250 };
+
struct joy
{
LPDIRECTINPUTDEVICE8 joy_handle;
@@ -140,7 +212,7 @@ struct OPENTRACK_EXPORT win32_joy_ctx
joy_handle = nullptr;
}
}
-
+
bool poll(fn f)
{
HRESULT hr;
@@ -189,53 +261,6 @@ struct OPENTRACK_EXPORT win32_joy_ctx
}
};
- static QString guid_to_string(const GUID guid)
- {
- char buf[40] = {0};
- wchar_t szGuidW[40] = {0};
-
- StringFromGUID2(guid, szGuidW, 40);
- WideCharToMultiByte(0, 0, szGuidW, -1, buf, 40, NULL, NULL);
-
- return QString(buf);
- }
-
- win32_joy_ctx()
- {
- refresh(true);
- return;
- }
-
- ~win32_joy_ctx()
- {
- release();
- }
-
- void release()
- {
- qDebug() << "release dinput handle";
- joys() = std::unordered_map<QString, std::shared_ptr<joy>>();
- {
- auto& di = dinput_handle();
- di->Release();
- di = nullptr;
- }
- }
-
- void refresh(bool first)
- {
- if (!first)
- {
- if (timer_joylist.elapsed_ms() < joylist_refresh_ms)
- return;
- timer_joylist.start();
- }
-
- enum_state st(joys(), first);
- }
-
- enum { joy_axis_size = 65535 };
-
struct enum_state
{
std::unordered_map<QString, std::shared_ptr<joy>>& joys;
@@ -338,13 +363,8 @@ end: return DIENUM_CONTINUE;
return DIENUM_CONTINUE;
}
};
-
- static LPDIRECTINPUT8& dinput_handle();
+
static std::unordered_map<QString, std::shared_ptr<joy>>& joys();
-
- QMutex mtx;
- Timer timer_joylist;
- enum { joylist_refresh_ms = 250 };
};
#endif