Skip to content

Commit

Permalink
Added PluginKit API for plugins to trigger hardware detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
godbyk committed Sep 23, 2016
1 parent 3139e8c commit 84b3a12
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 4 deletions.
4 changes: 3 additions & 1 deletion examples/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ set(OSVR_EXAMPLE_DEVICE_PLUGINS_SIMPLE
com_osvr_example_EyeTracker
com_osvr_example_Locomotion
com_osvr_example_MultipleAsync
org_osvr_example_Tracker)
org_osvr_example_Tracker
org_osvr_example_TriggerHardwareDetect)

# These are all the plugin targets: one listed only here need more careful configuration.
set(OSVR_EXAMPLE_DEVICE_PLUGINS
Expand Down Expand Up @@ -41,6 +42,7 @@ foreach(pluginname
com_osvr_example_EyeTracker
com_osvr_example_Locomotion
org_osvr_example_Tracker
org_osvr_example_TriggerHardwareDetect
com_osvr_example_MultipleAsync)
target_link_libraries(${pluginname} osvr_cxx11_flags)
endforeach()
Expand Down
114 changes: 114 additions & 0 deletions examples/plugin/org_osvr_example_TriggerHardwareDetect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/** @date 2016
@author
Sensics, Inc.
<http://sensics.com/osvr>
*/

// Copyright 2016 Sensics Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Internal Includes
#include <osvr/PluginKit/PluginKit.h>
#include <osvr/PluginKit/ButtonInterfaceC.h>
#include <osvr/Util/Log.h>
#include <osvr/Util/Logger.h>

// Library/third-party includes

// Standard includes
#include <iostream>
#include <chrono>
#include <thread>
#include <mutex>


// Anonymous namespace to avoid symbol collision
namespace {

/**
* @brief This fake device just triggers an autodetect every few seconds.
*/
class FakeDevice {
public:
FakeDevice(OSVR_PluginRegContext ctx) : m_context(ctx) {
/// Create the initialization options
OSVR_DeviceInitOptions opts = osvrDeviceCreateInitOptions(ctx);

// configure our tracker
osvrDeviceButtonConfigure(opts, &m_button, 1);

/// Create the device token with the options
m_dev.initAsync(ctx, "Hardware detection trigger", opts);

/// Send JSON descriptor
//m_dev.sendJsonDescriptor("{}");

/// Register update callback
m_dev.registerUpdateCallback(this);
}

OSVR_ReturnCode update() {
std::this_thread::sleep_for(std::chrono::seconds(3));

osvr::pluginkit::triggerHardwareDetect(m_context);

return OSVR_RETURN_SUCCESS;
}

private:
OSVR_PluginRegContext m_context;
osvr::pluginkit::DeviceToken m_dev;
OSVR_ButtonDeviceInterface m_button;
};

class HardwareDetection {
public:
HardwareDetection() : m_found(false), m_count(0), m_mutex()
{
logger_ = osvr::util::log::make_logger("org_osvr_example_TriggerHardwareDetect");
}

OSVR_ReturnCode operator()(OSVR_PluginRegContext ctx) {
std::lock_guard<std::mutex> guard(m_mutex);

m_count++;
logger_->info() << "Hardware detection triggered " << m_count << " times.";

if (m_found)
return OSVR_RETURN_SUCCESS;

osvr::pluginkit::registerObjectForDeletion(ctx, new FakeDevice(ctx));
m_found = true;

return OSVR_RETURN_SUCCESS;
}

private:
bool m_found;
int m_count;
std::mutex m_mutex;
osvr::util::log::LoggerPtr logger_;
};

} // namespace

OSVR_PLUGIN(org_osvr_example_TriggerHardwareDetect) {
osvr::pluginkit::PluginContext context(ctx);

/// Register a detection callback function object.
context.registerHardwareDetectCallback(new HardwareDetection());

return OSVR_RETURN_SUCCESS;
}

3 changes: 3 additions & 0 deletions inc/osvr/PluginHost/PluginSpecificRegistrationContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ namespace pluginhost {
/// @brief Accessor for plugin name.
OSVR_PLUGINHOST_EXPORT const std::string &getName() const;

/// @brief Trigger system-wide hardware detection.
OSVR_PLUGINHOST_EXPORT virtual void triggerHardwareDetect() = 0;

/// @brief Log a message to the plugin-specific channel.
///
/// @param severity The severity of the message.
Expand Down
6 changes: 6 additions & 0 deletions inc/osvr/PluginKit/PluginKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ namespace pluginkit {
return ::osvr::pluginkit::registerObjectForDeletion(m_ctx, obj);
}

/// @brief Triggers system-wide hardware detection.
/// @sa ::osvr::pluginkit::triggerHardwareDetect.
void triggerHardwareDetect() {
::osvr::pluginkit::triggerHardwareDetect(m_ctx);
}

/// @brief Log a message to the plugin-specific channel.
///
/// @param severity The severity of the log message.
Expand Down
14 changes: 13 additions & 1 deletion inc/osvr/PluginKit/PluginRegistration.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,24 @@ namespace pluginkit {
"registerDriverInstantiationCallback failed!");
}
}
/// @}

/// @brief Triggers system-wide hardware detection.
///
/// @param ctx The registration context passed to your entry point.
///
/// @sa PluginContext::triggerHardwareDetect
inline void triggerHardwareDetect(OSVR_PluginRegContext ctx) {
const OSVR_ReturnCode ret = osvrPluginTriggerHardwareDetect(ctx);
if (ret != OSVR_RETURN_SUCCESS) {
throw std::runtime_error("triggerHardwareDetect failed!");
}
}

inline void log(OSVR_PluginRegContext ctx, OSVR_LogLevel severity,
const char *message) {
osvrPluginLog(ctx, severity, message);
}
/// @}

} // namespace pluginkit
} // namespace osvr
Expand Down
14 changes: 13 additions & 1 deletion inc/osvr/PluginKit/PluginRegistrationC.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ OSVR_PLUGINKIT_EXPORT OSVR_ReturnCode osvrPluginRegisterDataWithDeleteCallback(
OSVR_INOUT_PTR void *pluginData) OSVR_FUNC_NONNULL((1, 2, 3));
/** @} */

/**
* @brief Trigger system-wide hardware detection.
*
* This causes each plugin's hardware detection callback to be called prior to
* the next run of the server main loop.
*
* @param ctx The registration context passed to your entry point.
*/
OSVR_PLUGINKIT_EXPORT OSVR_ReturnCode osvrPluginTriggerHardwareDetect(
OSVR_INOUT_PTR OSVR_PluginRegContext ctx) OSVR_FUNC_NONNULL((1));

/**
* @brief Log a message to the plugin's log channel.
*
Expand All @@ -160,4 +171,5 @@ OSVR_EXTERN_C_END

/** @} */

#endif
#endif /* INCLUDED_PluginRegistrationC_h_GUID_C019DFA9_5B54_4791_B0A4_040EA20501BA */

10 changes: 10 additions & 0 deletions src/osvr/PluginHost/PluginSpecificRegistrationContextImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

// Internal Includes
#include "PluginSpecificRegistrationContextImpl.h"
#include <osvr/PluginHost/RegistrationContext.h>
#include <osvr/Util/Verbosity.h>

// Library/third-party includes
Expand Down Expand Up @@ -169,5 +170,14 @@ namespace pluginhost {
util::AnyMap const &PluginSpecificRegistrationContextImpl::data() const {
return m_data;
}

void PluginSpecificRegistrationContextImpl::triggerHardwareDetect() {
if (m_parent == nullptr) {
throw std::logic_error(
"Can't access the registration context parent - it is null!");
}
m_parent->triggerHardwareDetect();
}

} // namespace pluginhost
} // namespace osvr
3 changes: 3 additions & 0 deletions src/osvr/PluginHost/PluginSpecificRegistrationContextImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ namespace pluginhost {
void *userData);
/// @}

/// @brief Trigger system-wide hardware detection.
virtual void triggerHardwareDetect();

private:
/// @brief Pointer with ownership semantics for deletion of plugin data.
typedef unique_ptr<void, OSVR_PluginDataDeleteCallback> PluginDataPtr;
Expand Down
17 changes: 16 additions & 1 deletion src/osvr/PluginKit/PluginRegistrationC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ OSVR_ReturnCode osvrPluginRegisterDataWithDeleteCallback(
return OSVR_RETURN_SUCCESS;
}

OSVR_ReturnCode
osvrPluginTriggerHardwareDetect(OSVR_INOUT_PTR OSVR_PluginRegContext ctx) {
OSVR_PLUGIN_HANDLE_NULL_CONTEXT("osvrPluginTriggerHardwareDetect", ctx);

try {
osvr::pluginhost::PluginSpecificRegistrationContext::get(ctx)
.triggerHardwareDetect();
} catch (const std::exception &e) {
std::cerr << "Error in osvrPluginTriggerHardwareDetectCallback - "
"caught exception reporting: "
<< e.what() << std::endl;
return OSVR_RETURN_FAILURE;
}
return OSVR_RETURN_SUCCESS;
}

void osvrPluginLog(OSVR_INOUT_PTR OSVR_PluginRegContext ctx,
OSVR_IN OSVR_LogLevel severity,
OSVR_IN const char *message) {
Expand All @@ -93,4 +109,3 @@ void osvrPluginLog(OSVR_INOUT_PTR OSVR_PluginRegContext ctx,
auto s = static_cast<osvr::util::log::LogLevel>(severity);
context->log(s, message);
}

0 comments on commit 84b3a12

Please sign in to comment.