Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/util/dinput_source.h
4212 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::VibrationMotorList EnumerateVibrationMotors(std::optional<InputBindingKey> for_device) override;
45
bool GetGenericBindingMapping(std::string_view device, GenericInputBindingMapping* mapping) override;
46
void UpdateMotorState(InputBindingKey key, float intensity) override;
47
void UpdateMotorState(InputBindingKey large_key, InputBindingKey small_key, float large_intensity,
48
float small_intensity) override;
49
50
bool ContainsDevice(std::string_view device) const override;
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, InputManager::BindingIconMappingFunction mapper) override;
54
55
std::unique_ptr<ForceFeedbackDevice> CreateForceFeedbackDevice(std::string_view device, Error* error) override;
56
57
private:
58
template<typename T>
59
using ComPtr = Microsoft::WRL::ComPtr<T>;
60
61
struct ControllerData
62
{
63
ComPtr<IDirectInputDevice8W> device;
64
DIJOYSTATE last_state = {};
65
GUID guid = {};
66
std::vector<u32> axis_offsets;
67
u32 num_buttons = 0;
68
69
// NOTE: We expose hats as num_buttons + (hat_index * 4) + direction.
70
u32 num_hats = 0;
71
72
bool needs_poll = true;
73
};
74
75
using ControllerDataArray = std::vector<ControllerData>;
76
77
static std::array<bool, NUM_HAT_DIRECTIONS> GetHatButtons(DWORD hat);
78
static std::string GetDeviceIdentifier(u32 index);
79
80
bool AddDevice(ControllerData& cd, const std::string& name);
81
82
void CheckForStateChanges(size_t index, const DIJOYSTATE& new_state);
83
84
ControllerDataArray m_controllers;
85
86
HMODULE m_dinput_module{};
87
ComPtr<IDirectInput8W> m_dinput;
88
LPCDIDATAFORMAT m_joystick_data_format{};
89
HWND m_toplevel_window = NULL;
90
};
91
92