duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

xinput_source.h (2411B)


      1 // SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
      2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
      3 
      4 #pragma once
      5 #include "common/windows_headers.h"
      6 #include "input_source.h"
      7 #include <Xinput.h>
      8 #include <array>
      9 #include <functional>
     10 #include <mutex>
     11 #include <vector>
     12 
     13 class SettingsInterface;
     14 
     15 class XInputSource final : public InputSource
     16 {
     17 public:
     18   enum : u32
     19   {
     20     NUM_CONTROLLERS = XUSER_MAX_COUNT, // 4
     21     NUM_BUTTONS = 15,
     22   };
     23 
     24   enum : u32
     25   {
     26     AXIS_LEFTX,
     27     AXIS_LEFTY,
     28     AXIS_RIGHTX,
     29     AXIS_RIGHTY,
     30     AXIS_LEFTTRIGGER,
     31     AXIS_RIGHTTRIGGER,
     32     NUM_AXES,
     33   };
     34 
     35   XInputSource();
     36   ~XInputSource();
     37 
     38   bool Initialize(SettingsInterface& si, std::unique_lock<std::mutex>& settings_lock) override;
     39   void UpdateSettings(SettingsInterface& si, std::unique_lock<std::mutex>& settings_lock) override;
     40   bool ReloadDevices() override;
     41   void Shutdown() override;
     42 
     43   void PollEvents() override;
     44   std::vector<std::pair<std::string, std::string>> EnumerateDevices() override;
     45   std::vector<InputBindingKey> EnumerateMotors() override;
     46   bool GetGenericBindingMapping(std::string_view device, GenericInputBindingMapping* mapping) override;
     47   void UpdateMotorState(InputBindingKey key, float intensity) override;
     48   void UpdateMotorState(InputBindingKey large_key, InputBindingKey small_key, float large_intensity,
     49                         float small_intensity) override;
     50 
     51   std::optional<InputBindingKey> ParseKeyString(std::string_view device, std::string_view binding) override;
     52   TinyString ConvertKeyToString(InputBindingKey key) override;
     53   TinyString ConvertKeyToIcon(InputBindingKey key) override;
     54 
     55 private:
     56   struct ControllerData
     57   {
     58     XINPUT_STATE last_state;
     59     XINPUT_VIBRATION last_vibration = {};
     60     bool connected = false;
     61     bool has_large_motor = false;
     62     bool has_small_motor = false;
     63   };
     64 
     65   using ControllerDataArray = std::array<ControllerData, NUM_CONTROLLERS>;
     66 
     67   void CheckForStateChanges(u32 index, const XINPUT_STATE& new_state);
     68   void HandleControllerConnection(u32 index);
     69   void HandleControllerDisconnection(u32 index);
     70 
     71   ControllerDataArray m_controllers;
     72 
     73   HMODULE m_xinput_module{};
     74   DWORD(WINAPI* m_xinput_get_state)(DWORD, XINPUT_STATE*) = nullptr;
     75   DWORD(WINAPI* m_xinput_set_state)(DWORD, XINPUT_VIBRATION*) = nullptr;
     76   DWORD(WINAPI* m_xinput_get_capabilities)(DWORD, DWORD, XINPUT_CAPABILITIES*) = nullptr;
     77 };