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 HIDGamepad_h 27#define HIDGamepad_h 28 29#if ENABLE(GAMEPAD) 30 31#include "PlatformGamepad.h" 32#include <IOKit/hid/IOHIDDevice.h> 33#include <wtf/HashMap.h> 34#include <wtf/RetainPtr.h> 35 36namespace WebCore { 37 38struct HIDGamepadElement { 39 HIDGamepadElement(double theMin, double theMax, IOHIDElementRef element) 40 : min(theMin) 41 , max(theMax) 42 , rawValue(theMin) 43 , iohidElement(element) 44 { 45 } 46 47 virtual ~HIDGamepadElement() 48 { 49 } 50 51 double min; 52 double max; 53 double rawValue; 54 RetainPtr<IOHIDElementRef> iohidElement; 55 56 virtual bool isButton() const { return false; } 57 virtual bool isAxis() const { return false; } 58 59 virtual double normalizedValue() = 0; 60}; 61 62struct HIDGamepadButton : HIDGamepadElement { 63 HIDGamepadButton(uint32_t thePriority, double min, double max, IOHIDElementRef element) 64 : HIDGamepadElement(min, max, element) 65 , priority(thePriority) 66 { 67 } 68 69 uint32_t priority; 70 71 virtual bool isButton() const override { return true; } 72 73 // Buttons normalize to the range (0.0) - (1.0) 74 virtual double normalizedValue() override 75 { 76 return (rawValue - min) / (max - min); 77 } 78}; 79 80struct HIDGamepadAxis : HIDGamepadElement { 81 HIDGamepadAxis(double min, double max, IOHIDElementRef element) 82 : HIDGamepadElement(min, max, element) 83 { 84 } 85 86 virtual bool isAxis() const override { return true; } 87 88 // Axes normalize to the range (-1.0) - (1.0) 89 virtual double normalizedValue() override 90 { 91 return (((rawValue - min) / (max - min)) * 2) - 1; 92 } 93}; 94 95class HIDGamepad : public PlatformGamepad { 96public: 97 HIDGamepad(IOHIDDeviceRef, unsigned index); 98 99 IOHIDDeviceRef hidDevice() const { return m_hidDevice.get(); } 100 101 void valueChanged(IOHIDValueRef); 102 103 virtual const Vector<double>& axisValues() const override final { return m_axisValues; } 104 virtual const Vector<double>& buttonValues() const override final { return m_buttonValues; } 105 106private: 107 void initElements(); 108 void initElementsFromArray(CFArrayRef); 109 110 bool maybeAddButton(IOHIDElementRef); 111 bool maybeAddAxis(IOHIDElementRef); 112 113 void getCurrentValueForElement(const HIDGamepadElement&); 114 115 RetainPtr<IOHIDDeviceRef> m_hidDevice; 116 117 HashMap<IOHIDElementCookie, HIDGamepadElement*> m_elementMap; 118 119 Vector<std::unique_ptr<HIDGamepadButton>> m_buttons; 120 Vector<std::unique_ptr<HIDGamepadAxis>> m_axes; 121 Vector<double> m_buttonValues; 122 Vector<double> m_axisValues; 123}; 124 125} // namespace WebCore 126 127#endif // ENABLE(GAMEPAD) 128#endif // HIDGamepad_h 129