Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/util/dinput_source.h
4802 views
1
// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <[email protected]>
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#pragma once
5
#define DIRECTINPUT_VERSION 0x0800
6
#include "common/windows_headers.h"
7
#include "core/types.h"
8
#include "input_source.h"
9
#include <array>
10
#include <dinput.h>
11
#include <functional>
12
#include <mutex>
13
#include <vector>
14
#include <wrl/client.h>
15
16
class DInputSource final : public InputSource
17
{
18
public:
19
enum HAT_DIRECTION : u32
20
{
21
HAT_DIRECTION_UP = 0,
22
HAT_DIRECTION_DOWN = 1,
23
HAT_DIRECTION_LEFT = 2,
24
HAT_DIRECTION_RIGHT = 3,
25
NUM_HAT_DIRECTIONS = 4,
26
};
27
28
enum : u32
29
{
30
MAX_NUM_BUTTONS = 32,
31
};
32
33
DInputSource();
34
~DInputSource() override;
35
36
bool Initialize(const SettingsInterface& si, std::unique_lock<std::mutex>& settings_lock) override;
37
void UpdateSettings(const SettingsInterface& si, std::unique_lock<std::mutex>& settings_lock) override;
38
bool ReloadDevices() override;
39
void Shutdown() override;
40
41
void PollEvents() override;
42
std::optional<float> GetCurrentValue(InputBindingKey key) override;
43
InputManager::DeviceList EnumerateDevices() override;
44
InputManager::DeviceEffectList EnumerateEffects(std::optional<InputBindingInfo::Type> type,
45
std::optional<InputBindingKey> for_device) 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
void UpdateLEDState(InputBindingKey key, float intensity) override;
51
52
bool ContainsDevice(std::string_view device) const override;
53
std::optional<InputBindingKey> ParseKeyString(std::string_view device, std::string_view binding) override;
54
TinyString ConvertKeyToString(InputBindingKey key) override;
55
TinyString ConvertKeyToIcon(InputBindingKey key, InputManager::BindingIconMappingFunction mapper) override;
56
57
std::unique_ptr<ForceFeedbackDevice> CreateForceFeedbackDevice(std::string_view device, Error* error) override;
58
59
private:
60
template<typename T>
61
using ComPtr = Microsoft::WRL::ComPtr<T>;
62
63
struct ControllerData
64
{
65
ComPtr<IDirectInputDevice8W> device;
66
DIJOYSTATE last_state = {};
67
GUID guid = {};
68
std::vector<u32> axis_offsets;
69
u32 num_buttons = 0;
70
71
// NOTE: We expose hats as num_buttons + (hat_index * 4) + direction.
72
u32 num_hats = 0;
73
74
bool needs_poll = true;
75
};
76
77
using ControllerDataArray = std::vector<ControllerData>;
78
79
static std::array<bool, NUM_HAT_DIRECTIONS> GetHatButtons(DWORD hat);
80
static std::string GetDeviceIdentifier(u32 index);
81
82
bool AddDevice(ControllerData& cd, const std::string& name);
83
84
void CheckForStateChanges(size_t index, const DIJOYSTATE& new_state);
85
86
ControllerDataArray m_controllers;
87
88
HMODULE m_dinput_module{};
89
ComPtr<IDirectInput8W> m_dinput;
90
LPCDIDATAFORMAT m_joystick_data_format{};
91
HWND m_toplevel_window = NULL;
92
};
93
94