Skip to content

Commit 2e4328a

Browse files
committed
Added PluginKit API for plugins to trigger hardware detection.
1 parent a309a49 commit 2e4328a

9 files changed

+175
-2
lines changed

examples/plugin/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ set(OSVR_EXAMPLE_DEVICE_PLUGINS_SIMPLE
66
com_osvr_example_EyeTracker
77
com_osvr_example_Locomotion
88
com_osvr_example_MultipleAsync
9-
org_osvr_example_Tracker)
9+
org_osvr_example_Tracker
10+
org_osvr_example_TriggerHardwareDetect)
1011

1112
# These are all the plugin targets: one listed only here need more careful configuration.
1213
set(OSVR_EXAMPLE_DEVICE_PLUGINS
@@ -40,6 +41,7 @@ foreach(pluginname
4041
com_osvr_example_Configured
4142
com_osvr_example_EyeTracker
4243
com_osvr_example_Locomotion
44+
org_osvr_example_TriggerHardwareDetect
4345
org_osvr_example_Tracker
4446
com_osvr_example_MultipleAsync)
4547
target_link_libraries(${pluginname} osvr_cxx11_flags)
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/** @date 2016
2+
@author
3+
Sensics, Inc.
4+
<http://sensics.com/osvr>
5+
*/
6+
7+
// Copyright 2016 Sensics Inc.
8+
//
9+
// Licensed under the Apache License, Version 2.0 (the "License");
10+
// you may not use this file except in compliance with the License.
11+
// You may obtain a copy of the License at
12+
//
13+
// http://www.apache.org/licenses/LICENSE-2.0
14+
//
15+
// Unless required by applicable law or agreed to in writing, software
16+
// distributed under the License is distributed on an "AS IS" BASIS,
17+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
// See the License for the specific language governing permissions and
19+
// limitations under the License.
20+
21+
// Internal Includes
22+
#include <osvr/PluginKit/PluginKit.h>
23+
#include <osvr/PluginKit/ButtonInterfaceC.h>
24+
25+
// Library/third-party includes
26+
27+
// Standard includes
28+
#include <iostream>
29+
#include <chrono>
30+
#include <thread>
31+
#include <mutex>
32+
33+
34+
// Anonymous namespace to avoid symbol collision
35+
namespace {
36+
37+
/**
38+
* @brief This fake device just triggers an autodetect every few seconds.
39+
*/
40+
class FakeDevice {
41+
public:
42+
FakeDevice(OSVR_PluginRegContext ctx) : m_context(ctx) {
43+
/// Create the initialization options
44+
OSVR_DeviceInitOptions opts = osvrDeviceCreateInitOptions(ctx);
45+
46+
// configure our tracker
47+
osvrDeviceButtonConfigure(opts, &m_button, 1);
48+
49+
/// Create the device token with the options
50+
m_dev.initAsync(ctx, "Hardware detection trigger", opts);
51+
52+
/// Send JSON descriptor
53+
//m_dev.sendJsonDescriptor("{}");
54+
55+
/// Register update callback
56+
m_dev.registerUpdateCallback(this);
57+
}
58+
59+
OSVR_ReturnCode update() {
60+
std::this_thread::sleep_for(std::chrono::seconds(3));
61+
62+
osvr::pluginkit::triggerHardwareDetect(m_context);
63+
64+
return OSVR_RETURN_SUCCESS;
65+
}
66+
67+
private:
68+
OSVR_PluginRegContext m_context;
69+
osvr::pluginkit::DeviceToken m_dev;
70+
OSVR_ButtonDeviceInterface m_button;
71+
};
72+
73+
class HardwareDetection {
74+
public:
75+
HardwareDetection() : m_found(false), m_count(0), m_mutex() {}
76+
77+
OSVR_ReturnCode operator()(OSVR_PluginRegContext ctx) {
78+
std::lock_guard<std::mutex> guard(m_mutex);
79+
80+
m_count++;
81+
std::cout << "[org_osvr_example_TriggerHardwareDetect] Hardware detection triggered " << m_count << " times." << std::endl;
82+
83+
if (m_found)
84+
return OSVR_RETURN_SUCCESS;
85+
86+
osvr::pluginkit::registerObjectForDeletion(ctx, new FakeDevice(ctx));
87+
m_found = true;
88+
89+
return OSVR_RETURN_SUCCESS;
90+
}
91+
92+
private:
93+
bool m_found;
94+
int m_count;
95+
std::mutex m_mutex;
96+
};
97+
98+
} // namespace
99+
100+
OSVR_PLUGIN(org_osvr_example_TriggerHardwareDetect) {
101+
102+
osvr::pluginkit::PluginContext context(ctx);
103+
104+
/// Register a detection callback function object.
105+
context.registerHardwareDetectCallback(new HardwareDetection());
106+
107+
return OSVR_RETURN_SUCCESS;
108+
}
109+

inc/osvr/PluginHost/PluginSpecificRegistrationContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ namespace pluginhost {
130130
/// @brief Accessor for plugin name.
131131
OSVR_PLUGINHOST_EXPORT const std::string &getName() const;
132132

133+
/// @brief Trigger system-wide hardware detection.
134+
OSVR_PLUGINHOST_EXPORT virtual void triggerHardwareDetect() = 0;
135+
133136
protected:
134137
/// @brief Constructor for derived class use only
135138
PluginSpecificRegistrationContext(std::string const &name);

inc/osvr/PluginKit/PluginKit.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ namespace pluginkit {
9393
return ::osvr::pluginkit::registerObjectForDeletion(m_ctx, obj);
9494
}
9595

96+
/// @brief Triggers system-wide hardware detection.
97+
/// @sa ::osvr::pluginkit::triggerHardwareDetect.
98+
void triggerHardwareDetect() {
99+
::osvr::pluginkit::triggerHardwareDetect(m_ctx);
100+
}
101+
96102
private:
97103
OSVR_PluginRegContext m_ctx;
98104
};

inc/osvr/PluginKit/PluginRegistration.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,18 @@ namespace pluginkit {
197197
"registerDriverInstantiationCallback failed!");
198198
}
199199
}
200+
201+
/// @brief Triggers system-wide hardware detection.
202+
///
203+
/// @param ctx The registration context passed to your entry point.
204+
///
205+
/// @sa PluginContext::triggerHardwareDetect
206+
inline void triggerHardwareDetect(OSVR_PluginRegContext ctx) {
207+
OSVR_ReturnCode ret = osvrPluginTriggerHardwareDetect(ctx);
208+
if (ret != OSVR_RETURN_SUCCESS) {
209+
throw std::runtime_error("triggerHardwareDetect failed!");
210+
}
211+
}
200212
/// @}
201213
} // namespace pluginkit
202214
} // namespace osvr

inc/osvr/PluginKit/PluginRegistrationC.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,19 @@ OSVR_PLUGINKIT_EXPORT OSVR_ReturnCode osvrPluginRegisterDataWithDeleteCallback(
143143
OSVR_INOUT_PTR void *pluginData) OSVR_FUNC_NONNULL((1, 2, 3));
144144
/** @} */
145145

146+
/** @brief Trigger system-wide hardware detection.
147+
148+
This causes each plugin's hardware detection callback to be called prior to
149+
the next run of the server main loop.
150+
151+
@param ctx The registration context passed to your entry point.
152+
*/
153+
OSVR_PLUGINKIT_EXPORT OSVR_ReturnCode osvrPluginTriggerHardwareDetect(
154+
OSVR_INOUT_PTR OSVR_PluginRegContext ctx) OSVR_FUNC_NONNULL((1));
155+
146156
OSVR_EXTERN_C_END
147157

148158
/** @} */
149159

150-
#endif
160+
#endif /* INCLUDED_PluginRegistrationC_h_GUID_C019DFA9_5B54_4791_B0A4_040EA20501BA */
161+

src/osvr/PluginHost/PluginSpecificRegistrationContextImpl.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
// Internal Includes
2828
#include "PluginSpecificRegistrationContextImpl.h"
29+
#include <osvr/PluginHost/RegistrationContext.h>
2930
#include <osvr/Util/Verbosity.h>
3031

3132
// Library/third-party includes
@@ -169,5 +170,14 @@ namespace pluginhost {
169170
util::AnyMap const &PluginSpecificRegistrationContextImpl::data() const {
170171
return m_data;
171172
}
173+
174+
void PluginSpecificRegistrationContextImpl::triggerHardwareDetect() {
175+
if (m_parent == nullptr) {
176+
throw std::logic_error(
177+
"Can't access the registration context parent - it is null!");
178+
}
179+
m_parent->triggerHardwareDetect();;
180+
}
181+
172182
} // namespace pluginhost
173183
} // namespace osvr

src/osvr/PluginHost/PluginSpecificRegistrationContextImpl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ namespace pluginhost {
115115
void *userData);
116116
/// @}
117117

118+
/// @brief Trigger system-wide hardware detection.
119+
virtual void triggerHardwareDetect();
120+
118121
private:
119122
/// @brief Pointer with ownership semantics for deletion of plugin data.
120123
typedef unique_ptr<void, OSVR_PluginDataDeleteCallback> PluginDataPtr;

src/osvr/PluginKit/PluginRegistrationC.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,20 @@ OSVR_ReturnCode osvrPluginRegisterDataWithDeleteCallback(
7979
context->registerDataWithDeleteCallback(deleteCallback, pluginData);
8080
return OSVR_RETURN_SUCCESS;
8181
}
82+
83+
OSVR_ReturnCode
84+
osvrPluginTriggerHardwareDetect(OSVR_INOUT_PTR OSVR_PluginRegContext ctx) {
85+
OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrPluginTriggerHardwareDetect", ctx);
86+
87+
try {
88+
osvr::pluginhost::PluginSpecificRegistrationContext::get(ctx)
89+
.triggerHardwareDetect();
90+
} catch (std::exception &e) {
91+
std::cerr << "Error in osvrPluginTriggerHardwareDetectCallback - "
92+
"caught exception reporting: "
93+
<< e.what() << std::endl;
94+
return OSVR_RETURN_FAILURE;
95+
}
96+
return OSVR_RETURN_SUCCESS;
97+
}
98+

0 commit comments

Comments
 (0)