| 1 | /* |
| 2 | * Copyright (C) 2017-2018 Igalia, S.L. All right reserved. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | #include "VRManager.h" |
| 23 | |
| 24 | #include "VRPlatformDisplay.h" |
| 25 | #include "VRPlatformManager.h" |
| 26 | |
| 27 | #if USE(OPENVR) |
| 28 | #include "VRPlatformManagerOpenVR.h" |
| 29 | #endif |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | VRManager& VRManager::singleton() |
| 34 | { |
| 35 | static NeverDestroyed<VRManager> instance; |
| 36 | return instance; |
| 37 | } |
| 38 | |
| 39 | VRManager::VRManager() |
| 40 | { |
| 41 | #if USE(OPENVR) |
| 42 | m_platformManager = VRPlatformManagerOpenVR::create(); |
| 43 | #endif |
| 44 | } |
| 45 | |
| 46 | VRManager::~VRManager() |
| 47 | { |
| 48 | m_platformManager = nullptr; |
| 49 | } |
| 50 | |
| 51 | Optional<VRManager::VRDisplaysVector> VRManager::getVRDisplays() |
| 52 | { |
| 53 | if (!m_platformManager) |
| 54 | return WTF::nullopt; |
| 55 | |
| 56 | auto displays = m_platformManager->getVRDisplays(); |
| 57 | VRDisplaysHashMap newDisplays; |
| 58 | for (auto& display : displays) { |
| 59 | ASSERT(display); |
| 60 | newDisplays.add(display->getDisplayInfo().displayIdentifier(), display); |
| 61 | |
| 62 | VRPlatformDisplayInfo info = display->getDisplayInfo(); |
| 63 | auto iterator = m_displays.find(info.displayIdentifier()); |
| 64 | if (iterator == m_displays.end()) |
| 65 | continue; |
| 66 | |
| 67 | display->updateDisplayInfo(); |
| 68 | VRPlatformDisplayInfo newInfo = display->getDisplayInfo(); |
| 69 | if (info.isConnected() != newInfo.isConnected()) |
| 70 | display->notifyVRPlatformDisplayEvent(newInfo.isConnected() ? VRPlatformDisplay::Event::Connected : VRPlatformDisplay::Event::Disconnected); |
| 71 | if (info.isMounted() != newInfo.isMounted()) |
| 72 | display->notifyVRPlatformDisplayEvent(newInfo.isMounted() ? VRPlatformDisplay::Event::Mounted : VRPlatformDisplay::Event::Unmounted); |
| 73 | } |
| 74 | |
| 75 | for (auto& iter : m_displays) { |
| 76 | ASSERT(iter.value); |
| 77 | if (!newDisplays.contains(iter.key)) |
| 78 | iter.value->notifyVRPlatformDisplayEvent(VRPlatformDisplay::Event::Disconnected); |
| 79 | } |
| 80 | |
| 81 | m_displays = WTFMove(newDisplays); |
| 82 | return displays; |
| 83 | } |
| 84 | |
| 85 | }; // namespace WebCore |
| 86 | |