Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/core/controller.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
6
#include "input_types.h"
7
#include "types.h"
8
9
#include <array>
10
#include <memory>
11
#include <optional>
12
#include <span>
13
#include <string>
14
#include <string_view>
15
#include <tuple>
16
#include <vector>
17
18
class SettingsInterface;
19
class StateWrapper;
20
21
class Controller
22
{
23
public:
24
struct ControllerBindingInfo
25
{
26
const char* name;
27
const char* display_name;
28
const char* icon_name;
29
u32 bind_index;
30
InputBindingInfo::Type type;
31
GenericInputBinding generic_mapping;
32
};
33
34
struct ControllerInfo
35
{
36
ControllerType type;
37
const char* name;
38
const char* display_name;
39
const char* icon_name;
40
std::span<const ControllerBindingInfo> bindings;
41
std::span<const SettingInfo> settings;
42
43
/// Returns localized controller type name.
44
std::string_view GetDisplayName() const;
45
46
/// Returns localized controller type name.
47
std::string_view GetBindingDisplayName(const ControllerBindingInfo& bi) const;
48
};
49
50
/// Default stick deadzone/sensitivity.
51
static constexpr float DEFAULT_STICK_DEADZONE = 0.0f;
52
static constexpr float DEFAULT_STICK_SENSITIVITY = 1.33f;
53
static constexpr float DEFAULT_BUTTON_DEADZONE = 0.25f;
54
55
explicit Controller(u32 index);
56
virtual ~Controller();
57
58
/// Returns the type of controller.
59
virtual ControllerType GetType() const = 0;
60
61
virtual void Reset();
62
virtual bool DoState(StateWrapper& sw, bool apply_input_state);
63
64
// Resets all state for the transferring to/from the device.
65
virtual void ResetTransferState();
66
67
// Returns the value of ACK, as well as filling out_data.
68
virtual bool Transfer(const u8 data_in, u8* data_out);
69
70
/// Changes the specified axis state. Values are normalized from -1..1.
71
virtual float GetBindState(u32 index) const;
72
73
/// Changes the specified bind state. Values are normalized from -1..1.
74
virtual void SetBindState(u32 index, float value);
75
76
/// Returns a bitmask of the current button states, 1 = on.
77
virtual u32 GetButtonStateBits() const;
78
79
/// Returns analog input bytes packed as a u32. Values are specific to controller type.
80
virtual std::optional<u32> GetAnalogInputBytes() const;
81
82
/// Loads/refreshes any per-controller settings.
83
virtual void LoadSettings(const SettingsInterface& si, const char* section, bool initial);
84
85
/// Creates a new controller of the specified type.
86
static std::unique_ptr<Controller> Create(ControllerType type, u32 index);
87
88
/// Returns a list of all controller types.
89
static const std::array<const ControllerInfo*, static_cast<size_t>(ControllerType::Count)>& GetControllerInfoList();
90
91
/// Returns general information for the specified controller type.
92
static const ControllerInfo& GetControllerInfo(ControllerType type);
93
static const ControllerInfo* GetControllerInfo(std::string_view name);
94
95
/// Applies an analog deadzone/sensitivity.
96
static float ApplyAnalogDeadzoneSensitivity(float deadzone, float sensitivity, float value)
97
{
98
return (value < deadzone) ? 0.0f : ((value - deadzone) / (1.0f - deadzone) * sensitivity);
99
}
100
101
/// Returns true if the specified coordinates are inside a circular deadzone.
102
static bool InCircularDeadzone(float deadzone, float pos_x, float pos_y);
103
104
/// Converts a global pad index to a multitap port and slot.
105
static std::tuple<u32, u32> ConvertPadToPortAndSlot(u32 index);
106
107
/// Converts a multitap port and slot to a global pad index.
108
static u32 ConvertPortAndSlotToPad(u32 port, u32 slot);
109
110
/// Returns true if the given pad index is a multitap slot.
111
static bool PadIsMultitapSlot(u32 index);
112
static bool PortAndSlotIsMultitap(u32 port, u32 slot);
113
114
/// Returns the configuration section for the specified gamepad.
115
static std::string GetSettingsSection(u32 pad);
116
117
/// Returns a printable label for a given port.
118
static const char* GetPortDisplayName(u32 port, u32 slot, bool mtap);
119
static const char* GetPortDisplayName(u32 index);
120
121
/// List of controller indices in the order that they should be displayed.
122
static const std::array<u32, NUM_CONTROLLER_AND_CARD_PORTS> PortDisplayOrder;
123
124
/// Returns true if automatic analog mode can be used.
125
static bool CanStartInAnalogMode(ControllerType ctype);
126
127
protected:
128
u32 m_index;
129
};
130
131