HIDGamepadProvider.h [plain text]
#pragma once
#if ENABLE(GAMEPAD) && PLATFORM(MAC)
#include "GamepadProvider.h"
#include "HIDGamepad.h"
#include "Timer.h"
#include <IOKit/hid/IOHIDManager.h>
#include <pal/spi/mac/IOKitSPIMac.h>
#include <wtf/Deque.h>
#include <wtf/HashMap.h>
#include <wtf/RetainPtr.h>
#include <wtf/WorkQueue.h>
namespace WebCore {
class GamepadProviderClient;
class HIDGamepadProvider : public GamepadProvider {
WTF_MAKE_NONCOPYABLE(HIDGamepadProvider);
friend class NeverDestroyed<HIDGamepadProvider>;
public:
WEBCORE_EXPORT static HIDGamepadProvider& singleton();
WEBCORE_EXPORT void startMonitoringGamepads(GamepadProviderClient&) final;
WEBCORE_EXPORT void stopMonitoringGamepads(GamepadProviderClient&) final;
const Vector<PlatformGamepad*>& platformGamepads() final { return m_gamepadVector; }
WEBCORE_EXPORT void stopMonitoringInput();
WEBCORE_EXPORT void startMonitoringInput();
void deviceAdded(IOHIDDeviceRef);
void deviceRemoved(IOHIDDeviceRef);
void valuesChanged(IOHIDValueRef);
void ignoreGameControllerFrameworkDevices() { m_ignoresGameControllerFrameworkDevices = true; }
size_t numberOfConnectedGamepads() const { return m_gamepadMap.size(); };
private:
HIDGamepadProvider();
std::unique_ptr<HIDGamepad> removeGamepadForDevice(IOHIDDeviceRef);
void openAndScheduleManager();
void closeAndUnscheduleManager();
void initialGamepadsConnectedTimerFired();
void inputNotificationTimerFired();
unsigned indexForNewlyConnectedDevice();
Vector<PlatformGamepad*> m_gamepadVector;
HashMap<IOHIDDeviceRef, std::unique_ptr<HIDGamepad>> m_gamepadMap;
RetainPtr<IOHIDManagerRef> m_manager;
bool m_initialGamepadsConnected { false };
bool m_ignoresGameControllerFrameworkDevices { false };
Timer m_initialGamepadsConnectedTimer;
Timer m_inputNotificationTimer;
#if HAVE(MULTIGAMEPADPROVIDER_SUPPORT)
HashSet<IOHIDDeviceRef> m_gameControllerManagedGamepads;
#endif // HAVE(MULTIGAMEPADPROVIDER_SUPPORT)
};
}
#endif // ENABLE(GAMEPAD) && PLATFORM(MAC)