/******************************************************************************* * Copyright 2009-2016 Jörg Müller * * 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. ******************************************************************************/ #include "devices/DeviceManager.h" #include "devices/IDeviceFactory.h" #include "devices/IDevice.h" #include "devices/I3DDevice.h" #include #include #include AUD_NAMESPACE_BEGIN std::unordered_map> DeviceManager::m_factories; std::shared_ptr DeviceManager::m_device; void DeviceManager::registerDevice(std::string name, std::shared_ptr factory) { m_factories[name] = factory; } std::shared_ptr DeviceManager::getDeviceFactory(std::string name) { return m_factories[name]; } std::shared_ptr DeviceManager::getDefaultDeviceFactory() { int min = std::numeric_limits::min(); std::shared_ptr result; for(auto factory : m_factories) { if(factory.second->getPriority() >= min) { result = factory.second; min = result->getPriority(); } } return result; } void DeviceManager::setDevice(std::shared_ptr device) { m_device = device; } void DeviceManager::openDevice(std::string name) { setDevice(getDeviceFactory(name)->openDevice()); } void DeviceManager::openDefaultDevice() { setDevice(getDefaultDeviceFactory()->openDevice()); } void DeviceManager::releaseDevice() { m_device = nullptr; } std::shared_ptr DeviceManager::getDevice() { return m_device; } std::shared_ptr DeviceManager::get3DDevice() { return std::dynamic_pointer_cast(m_device); } std::vector DeviceManager::getAvailableDeviceNames() { struct DeviceNamePriority { std::string name; int priority; }; std::vector devices; devices.reserve(m_factories.size()); for(const auto& pair : m_factories) devices.push_back({pair.first, pair.second->getPriority()}); auto sort = [](const DeviceNamePriority& lhs, const DeviceNamePriority& rhs){ return lhs.priority > rhs.priority; }; std::sort(devices.begin(), devices.end(), sort); std::vector names; names.reserve(devices.size()); for(const auto& device : devices) names.push_back(device.name); return names; } AUD_NAMESPACE_END