Skip to content

Commit 84b3a12

Browse files
committed
Added PluginKit API for plugins to trigger hardware detection.
1 parent 3139e8c commit 84b3a12

File tree

9 files changed

+181
-4
lines changed

9 files changed

+181
-4
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
@@ -41,6 +42,7 @@ foreach(pluginname
4142
com_osvr_example_EyeTracker
4243
com_osvr_example_Locomotion
4344
org_osvr_example_Tracker
45+
org_osvr_example_TriggerHardwareDetect
4446
com_osvr_example_MultipleAsync)
4547
target_link_libraries(${pluginname} osvr_cxx11_flags)
4648
endforeach()
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
#include <osvr/Util/Log.h>
25+
#include <osvr/Util/Logger.h>
26+
27+
// Library/third-party includes
28+
29+
// Standard includes
30+
#include <iostream>
31+
#include <chrono>
32+
#include <thread>
33+
#include <mutex>
34+
35+
36+
// Anonymous namespace to avoid symbol collision
37+
namespace {
38+
39+
/**
40+
* @brief This fake device just triggers an autodetect every few seconds.
41+
*/
42+
class FakeDevice {
43+
public:
44+
FakeDevice(OSVR_PluginRegContext ctx) : m_context(ctx) {
45+
/// Create the initialization options
46+
OSVR_DeviceInitOptions opts = osvrDeviceCreateInitOptions(ctx);
47+
48+
// configure our tracker
49+
osvrDeviceButtonConfigure(opts, &m_button, 1);
50+
51+
/// Create the device token with the options
52+
m_dev.initAsync(ctx, "Hardware detection trigger", opts);
53+
54+
/// Send JSON descriptor
55+
//m_dev.sendJsonDescriptor("{}");
56+
57+
/// Register update callback
58+
m_dev.registerUpdateCallback(this);
59+
}
60+
61+
OSVR_ReturnCode update() {
62+
std::this_thread::sleep_for(std::chrono::seconds(3));
63+
64+
osvr::pluginkit::triggerHardwareDetect(m_context);
65+
66+
return OSVR_RETURN_SUCCESS;
67+
}
68+
69+
private:
70+
OSVR_PluginRegContext m_context;
71+
osvr::pluginkit::DeviceToken m_dev;
72+
OSVR_ButtonDeviceInterface m_button;
73+
};
74+
75+
class HardwareDetection {
76+
public:
77+
HardwareDetection() : m_found(false), m_count(0), m_mutex()
78+
{
79+
logger_ = osvr::util::log::make_logger("org_osvr_example_TriggerHardwareDetect");
80+
}
81+
82+
OSVR_ReturnCode operator()(OSVR_PluginRegContext ctx) {
83+
std::lock_guard<std::mutex> guard(m_mutex);
84+
85+
m_count++;
86+
logger_->info() << "Hardware detection triggered " << m_count << " times.";
87+
88+
if (m_found)
89+
return OSVR_RETURN_SUCCESS;
90+
91+
osvr::pluginkit::registerObjectForDeletion(ctx, new FakeDevice(ctx));
92+
m_found = true;
93+
94+
return OSVR_RETURN_SUCCESS;
95+
}
96+
97+
private:
98+
bool m_found;
99+
int m_count;
100+
std::mutex m_mutex;
101+
osvr::util::log::LoggerPtr logger_;
102+
};
103+
104+
} // namespace
105+
106+
OSVR_PLUGIN(org_osvr_example_TriggerHardwareDetect) {
107+
osvr::pluginkit::PluginContext context(ctx);
108+
109+
/// Register a detection callback function object.
110+
context.registerHardwareDetectCallback(new HardwareDetection());
111+
112+
return OSVR_RETURN_SUCCESS;
113+
}
114+

inc/osvr/PluginHost/PluginSpecificRegistrationContext.h

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

135+
/// @brief Trigger system-wide hardware detection.
136+
OSVR_PLUGINHOST_EXPORT virtual void triggerHardwareDetect() = 0;
137+
135138
/// @brief Log a message to the plugin-specific channel.
136139
///
137140
/// @param severity The severity of the message.

inc/osvr/PluginKit/PluginKit.h

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

97+
/// @brief Triggers system-wide hardware detection.
98+
/// @sa ::osvr::pluginkit::triggerHardwareDetect.
99+
void triggerHardwareDetect() {
100+
::osvr::pluginkit::triggerHardwareDetect(m_ctx);
101+
}
102+
97103
/// @brief Log a message to the plugin-specific channel.
98104
///
99105
/// @param severity The severity of the log message.

inc/osvr/PluginKit/PluginRegistration.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,24 @@ namespace pluginkit {
197197
"registerDriverInstantiationCallback failed!");
198198
}
199199
}
200-
/// @}
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+
const OSVR_ReturnCode ret = osvrPluginTriggerHardwareDetect(ctx);
208+
if (ret != OSVR_RETURN_SUCCESS) {
209+
throw std::runtime_error("triggerHardwareDetect failed!");
210+
}
211+
}
201212

202213
inline void log(OSVR_PluginRegContext ctx, OSVR_LogLevel severity,
203214
const char *message) {
204215
osvrPluginLog(ctx, severity, message);
205216
}
217+
/// @}
206218

207219
} // namespace pluginkit
208220
} // namespace osvr

inc/osvr/PluginKit/PluginRegistrationC.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ OSVR_PLUGINKIT_EXPORT OSVR_ReturnCode osvrPluginRegisterDataWithDeleteCallback(
144144
OSVR_INOUT_PTR void *pluginData) OSVR_FUNC_NONNULL((1, 2, 3));
145145
/** @} */
146146

147+
/**
148+
* @brief Trigger system-wide hardware detection.
149+
*
150+
* This causes each plugin's hardware detection callback to be called prior to
151+
* the next run of the server main loop.
152+
*
153+
* @param ctx The registration context passed to your entry point.
154+
*/
155+
OSVR_PLUGINKIT_EXPORT OSVR_ReturnCode osvrPluginTriggerHardwareDetect(
156+
OSVR_INOUT_PTR OSVR_PluginRegContext ctx) OSVR_FUNC_NONNULL((1));
157+
147158
/**
148159
* @brief Log a message to the plugin's log channel.
149160
*
@@ -160,4 +171,5 @@ OSVR_EXTERN_C_END
160171

161172
/** @} */
162173

163-
#endif
174+
#endif /* INCLUDED_PluginRegistrationC_h_GUID_C019DFA9_5B54_4791_B0A4_040EA20501BA */
175+

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: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ OSVR_ReturnCode osvrPluginRegisterDataWithDeleteCallback(
8080
return OSVR_RETURN_SUCCESS;
8181
}
8282

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 (const 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+
8399
void osvrPluginLog(OSVR_INOUT_PTR OSVR_PluginRegContext ctx,
84100
OSVR_IN OSVR_LogLevel severity,
85101
OSVR_IN const char *message) {
@@ -93,4 +109,3 @@ void osvrPluginLog(OSVR_INOUT_PTR OSVR_PluginRegContext ctx,
93109
auto s = static_cast<osvr::util::log::LogLevel>(severity);
94110
context->log(s, message);
95111
}
96-

0 commit comments

Comments
 (0)