1/*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef HIDGamepadProvider_h
27#define HIDGamepadProvider_h
28
29#if ENABLE(GAMEPAD)
30
31#include "GamepadProvider.h"
32#include "HIDGamepad.h"
33#include "Timer.h"
34#include <IOKit/hid/IOHIDManager.h>
35#include <wtf/Deque.h>
36#include <wtf/HashMap.h>
37#include <wtf/HashSet.h>
38#include <wtf/NeverDestroyed.h>
39#include <wtf/RetainPtr.h>
40
41namespace WebCore {
42
43class GamepadProviderClient;
44
45class HIDGamepadProvider : public GamepadProvider {
46    WTF_MAKE_NONCOPYABLE(HIDGamepadProvider);
47    friend class NeverDestroyed<HIDGamepadProvider>;
48public:
49    static HIDGamepadProvider& shared();
50
51    virtual void startMonitoringGamepads(GamepadProviderClient*);
52    virtual void stopMonitoringGamepads(GamepadProviderClient*);
53    virtual const Vector<PlatformGamepad*>& platformGamepads() { return m_gamepadVector; }
54
55    void deviceAdded(IOHIDDeviceRef);
56    void deviceRemoved(IOHIDDeviceRef);
57    void valuesChanged(IOHIDValueRef);
58
59private:
60    HIDGamepadProvider();
61
62    std::unique_ptr<HIDGamepad> removeGamepadForDevice(IOHIDDeviceRef);
63
64    void openAndScheduleManager();
65    void closeAndUnscheduleManager();
66
67    void connectionDelayTimerFired(Timer<HIDGamepadProvider>&);
68    void inputNotificationTimerFired(Timer<HIDGamepadProvider>&);
69
70    unsigned indexForNewlyConnectedDevice();
71
72    Vector<PlatformGamepad*> m_gamepadVector;
73    HashMap<IOHIDDeviceRef, std::unique_ptr<HIDGamepad>> m_gamepadMap;
74
75    RetainPtr<IOHIDManagerRef> m_manager;
76
77    HashSet<GamepadProviderClient*> m_clients;
78    bool m_shouldDispatchCallbacks;
79
80    Timer<HIDGamepadProvider> m_connectionDelayTimer;
81    Timer<HIDGamepadProvider> m_inputNotificationTimer;
82};
83
84} // namespace WebCore
85
86#endif // ENABLE(GAMEPAD)
87#endif // HIDGamepadProvider_h
88