| 1 | // Copyright 2010 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/ControllerInterface/XInput/XInput.h" |
| 5 | |
| 6 | #ifndef XINPUT_GAMEPAD_GUIDE |
| 7 | #define XINPUT_GAMEPAD_GUIDE 0x0400 |
| 8 | #endif |
| 9 | |
| 10 | namespace ciface::XInput |
| 11 | { |
| 12 | struct ButtonDef |
| 13 | { |
| 14 | const char* name; |
| 15 | WORD bitmask; |
| 16 | }; |
| 17 | static constexpr std::array<ButtonDef, 15> named_buttons{{ |
| 18 | {"Button A", XINPUT_GAMEPAD_A}, |
| 19 | {"Button B", XINPUT_GAMEPAD_B}, |
| 20 | {"Button X", XINPUT_GAMEPAD_X}, |
| 21 | {"Button Y", XINPUT_GAMEPAD_Y}, |
| 22 | {"Pad N", XINPUT_GAMEPAD_DPAD_UP}, |
| 23 | {"Pad S", XINPUT_GAMEPAD_DPAD_DOWN}, |
| 24 | {"Pad W", XINPUT_GAMEPAD_DPAD_LEFT}, |
| 25 | {"Pad E", XINPUT_GAMEPAD_DPAD_RIGHT}, |
| 26 | {"Start", XINPUT_GAMEPAD_START}, |
| 27 | {"Back", XINPUT_GAMEPAD_BACK}, |
| 28 | {"Shoulder L", XINPUT_GAMEPAD_LEFT_SHOULDER}, |
| 29 | {"Shoulder R", XINPUT_GAMEPAD_RIGHT_SHOULDER}, |
| 30 | {"Guide", XINPUT_GAMEPAD_GUIDE}, |
| 31 | {"Thumb L", XINPUT_GAMEPAD_LEFT_THUMB}, |
| 32 | {"Thumb R", XINPUT_GAMEPAD_RIGHT_THUMB}, |
| 33 | }}; |
| 34 | |
| 35 | static constexpr std::array named_triggers{"Trigger L", "Trigger R"}; |
| 36 | |
| 37 | static constexpr std::array named_axes{"Left X", "Left Y", "Right X", "Right Y"}; |
| 38 | |
| 39 | static constexpr std::array named_motors{"Motor L", "Motor R"}; |
| 40 | |
| 41 | class Button final : public Core::Device::Input |
| 42 | { |
| 43 | public: |
| 44 | Button(u8 index, const WORD& buttons) : m_buttons(buttons), m_index(index) {} |
| 45 | std::string GetName() const override { return named_buttons[m_index].name; } |
| 46 | ControlState GetState() const override |
| 47 | { |
| 48 | return (m_buttons & named_buttons[m_index].bitmask) > 0; |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | const WORD& m_buttons; |
| 53 | const u8 m_index; |
| 54 | }; |
| 55 | |
| 56 | class Axis final : public Core::Device::Input |
| 57 | { |
| 58 | public: |
| 59 | Axis(u8 index, const SHORT& axis, SHORT range) : m_axis(axis), m_range(range), m_index(index) {} |
| 60 | std::string GetName() const override |
| 61 | { |
| 62 | return std::string(named_axes[m_index]) + (m_range < 0 ? '-' : '+'); |
| 63 | } |
| 64 | ControlState GetState() const override { return ControlState(m_axis) / m_range; } |
| 65 | |
| 66 | private: |
| 67 | const SHORT& m_axis; |
| 68 | const SHORT m_range; |
| 69 | const u8 m_index; |
| 70 | }; |
| 71 | |
| 72 | class Trigger final : public Core::Device::Input |
| 73 | { |
| 74 | public: |
| 75 | Trigger(u8 index, const BYTE& trigger, BYTE range) |
| 76 | : m_trigger(trigger), m_range(range), m_index(index) |
| 77 | { |
| 78 | } |
| 79 | std::string GetName() const override { return named_triggers[m_index]; } |
| 80 | ControlState GetState() const override { return ControlState(m_trigger) / m_range; } |
| 81 | |
| 82 | private: |
| 83 | const BYTE& m_trigger; |
| 84 | const BYTE m_range; |
| 85 | const u8 m_index; |
| 86 | }; |
| 87 | |
| 88 | class Motor final : public Core::Device::Output |
| 89 | { |
| 90 | public: |
| 91 | Motor(u8 index, Device* parent, WORD& motor, WORD range) |
| 92 | : m_motor(motor), m_range(range), m_index(index), m_parent(parent) |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | std::string GetName() const override { return named_motors[m_index]; } |
| 97 | void SetState(ControlState state) override |
| 98 | { |
| 99 | const auto old_value = m_motor; |
| 100 | m_motor = (WORD)(state * m_range); |
| 101 | |
| 102 | // Only update if the state changed. |
| 103 | if (m_motor != old_value) |
| 104 | m_parent->UpdateMotors(); |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | WORD& m_motor; |
| 109 | const WORD m_range; |
| 110 | const u8 m_index; |
| 111 | Device* m_parent; |
| 112 | }; |
| 113 | |
| 114 | class Battery final : public Core::Device::Input |
| 115 | { |
| 116 | public: |
| 117 | Battery(const ControlState* level) : m_level(*level) {} |
| 118 | std::string GetName() const override { return "Battery"; } |
| 119 | ControlState GetState() const override { return m_level; } |
| 120 | bool IsDetectable() const override { return false; } |
| 121 | |
| 122 | private: |
| 123 | const ControlState& m_level; |
| 124 | }; |
| 125 | |
| 126 | static HMODULE hXInput = nullptr; |
| 127 | |
| 128 | typedef decltype(&XInputGetCapabilities) XInputGetCapabilities_t; |
| 129 | typedef decltype(&XInputSetState) XInputSetState_t; |
| 130 | typedef decltype(&XInputGetState) XInputGetState_t; |
| 131 | typedef decltype(&XInputGetBatteryInformation) XInputGetBatteryInformation_t; |
| 132 | |
| 133 | static XInputGetCapabilities_t PXInputGetCapabilities = nullptr; |
| 134 | static XInputSetState_t PXInputSetState = nullptr; |
| 135 | static XInputGetState_t PXInputGetState = nullptr; |
| 136 | static XInputGetBatteryInformation_t PXInputGetBatteryInformation = nullptr; |
| 137 | |
| 138 | static bool s_have_guide_button = false; |
| 139 | |
| 140 | void Init() |
| 141 | { |
| 142 | if (!hXInput) |
| 143 | { |
| 144 | // Try for the most recent version we were compiled against (will only work if running on Win8+) |
| 145 | hXInput = ::LoadLibrary(XINPUT_DLL); |
| 146 | if (!hXInput) |
| 147 | { |
| 148 | // Drop back to DXSDK June 2010 version. Requires DX June 2010 redist. |
| 149 | hXInput = ::LoadLibrary(TEXT("xinput1_3.dll")); |
| 150 | if (!hXInput) |
| 151 | { |
| 152 | return; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | PXInputGetCapabilities = |
| 157 | (XInputGetCapabilities_t)::GetProcAddress(hXInput, "XInputGetCapabilities"); |
| 158 | PXInputSetState = (XInputSetState_t)::GetProcAddress(hXInput, "XInputSetState"); |
| 159 | PXInputGetBatteryInformation = |
| 160 | (XInputGetBatteryInformation_t)::GetProcAddress(hXInput, "XInputGetBatteryInformation"); |
| 161 | |
| 162 | // Ordinal 100 is the same as XInputGetState, except it doesn't dummy out the guide |
| 163 | // button info. Try loading it and fall back if needed. |
| 164 | PXInputGetState = (XInputGetState_t)::GetProcAddress(hXInput, (LPCSTR)100); |
| 165 | |
| 166 | s_have_guide_button = PXInputGetState != nullptr; |
| 167 | |
| 168 | if (!PXInputGetState) |
| 169 | PXInputGetState = (XInputGetState_t)::GetProcAddress(hXInput, "XInputGetState"); |
| 170 | |
| 171 | if (!PXInputGetCapabilities || !PXInputSetState || !PXInputGetState || |
| 172 | !PXInputGetBatteryInformation) |
| 173 | { |
| 174 | ::FreeLibrary(hXInput); |
| 175 | hXInput = nullptr; |
| 176 | return; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void PopulateDevices() |
| 182 | { |
| 183 | if (!hXInput) |
| 184 | return; |
| 185 | |
| 186 | g_controller_interface.RemoveDevice([](const auto* dev) { return dev->GetSource() == "XInput"; }); |
| 187 | |
| 188 | XINPUT_CAPABILITIES caps; |
| 189 | for (int i = 0; i != 4; ++i) |
| 190 | if (ERROR_SUCCESS == PXInputGetCapabilities(i, 0, &caps)) |
| 191 | g_controller_interface.AddDevice(std::make_shared<Device>(caps, i)); |
| 192 | } |
| 193 | |
| 194 | void DeInit() |
| 195 | { |
| 196 | if (hXInput) |
| 197 | { |
| 198 | ::FreeLibrary(hXInput); |
| 199 | hXInput = nullptr; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | Device::Device(const XINPUT_CAPABILITIES& caps, u8 index) : m_subtype(caps.SubType), m_index(index) |
| 204 | { |
| 205 | // XInputGetCaps can be broken on some devices, so we'll just ignore it |
| 206 | // and assume all gamepad + vibration capabilities are supported |
| 207 | |
| 208 | // Buttons. |
| 209 | for (size_t i = 0; i != size(named_buttons); ++i) |
| 210 | { |
| 211 | // Only add guide button if we have the 100 ordinal XInputGetState. |
| 212 | if (named_buttons[i].bitmask == XINPUT_GAMEPAD_GUIDE && !s_have_guide_button) |
| 213 | continue; |
| 214 | |
| 215 | AddInput(new Button(u8(i), m_state_in.Gamepad.wButtons)); |
| 216 | } |
| 217 | |
| 218 | // Triggers. |
| 219 | for (size_t i = 0; i != size(named_triggers); ++i) |
| 220 | AddInput(new Trigger(u8(i), (&m_state_in.Gamepad.bLeftTrigger)[i], 255)); |
| 221 | |
| 222 | // Axes. |
| 223 | for (size_t i = 0; i != size(named_axes); ++i) |
| 224 | { |
| 225 | const SHORT& ax = (&m_state_in.Gamepad.sThumbLX)[i]; |
| 226 | |
| 227 | // Each axis gets a negative and a positive input instance associated with it. |
| 228 | AddInput(new Axis(u8(i), ax, -32768)); |
| 229 | AddInput(new Axis(u8(i), ax, 32767)); |
| 230 | } |
| 231 | |
| 232 | // Rumble motors. |
| 233 | for (size_t i = 0; i != size(named_motors); ++i) |
| 234 | AddOutput(new Motor(u8(i), this, (&m_state_out.wLeftMotorSpeed)[i], 65535)); |
| 235 | |
| 236 | AddInput(new Battery(&m_battery_level)); |
| 237 | } |
| 238 | |
| 239 | std::string Device::GetName() const |
| 240 | { |
| 241 | switch (m_subtype) |
| 242 | { |
| 243 | case XINPUT_DEVSUBTYPE_GAMEPAD: |
| 244 | return "Gamepad"; |
| 245 | case XINPUT_DEVSUBTYPE_WHEEL: |
| 246 | return "Wheel"; |
| 247 | case XINPUT_DEVSUBTYPE_ARCADE_STICK: |
| 248 | return "Arcade Stick"; |
| 249 | case XINPUT_DEVSUBTYPE_FLIGHT_STICK: |
| 250 | return "Flight Stick"; |
| 251 | case XINPUT_DEVSUBTYPE_DANCE_PAD: |
| 252 | return "Dance Pad"; |
| 253 | case XINPUT_DEVSUBTYPE_GUITAR: |
| 254 | return "Guitar"; |
| 255 | case XINPUT_DEVSUBTYPE_DRUM_KIT: |
| 256 | return "Drum Kit"; |
| 257 | default: |
| 258 | return "Device"; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | std::string Device::GetSource() const |
| 263 | { |
| 264 | return "XInput"; |
| 265 | } |
| 266 | |
| 267 | Core::DeviceRemoval Device::UpdateInput() |
| 268 | { |
| 269 | PXInputGetState(m_index, &m_state_in); |
| 270 | |
| 271 | XINPUT_BATTERY_INFORMATION battery_info = {}; |
| 272 | if (SUCCEEDED(PXInputGetBatteryInformation(m_index, BATTERY_DEVTYPE_GAMEPAD, &battery_info))) |
| 273 | { |
| 274 | switch (battery_info.BatteryType) |
| 275 | { |
| 276 | case BATTERY_TYPE_DISCONNECTED: |
| 277 | case BATTERY_TYPE_UNKNOWN: |
| 278 | m_battery_level = 0; |
| 279 | break; |
| 280 | case BATTERY_TYPE_WIRED: |
| 281 | m_battery_level = BATTERY_INPUT_MAX_VALUE; |
| 282 | break; |
| 283 | default: |
| 284 | m_battery_level = |
| 285 | battery_info.BatteryLevel / ControlState(BATTERY_LEVEL_FULL) * BATTERY_INPUT_MAX_VALUE; |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return Core::DeviceRemoval::Keep; |
| 291 | } |
| 292 | |
| 293 | void Device::UpdateMotors() |
| 294 | { |
| 295 | PXInputSetState(m_index, &m_state_out); |
| 296 | } |
| 297 | |
| 298 | std::optional<int> Device::GetPreferredId() const |
| 299 | { |
| 300 | return m_index; |
| 301 | } |
| 302 | |
| 303 | } // namespace ciface::XInput |