|
| 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 | + |
0 commit comments