Repository navigation
Expand file tree
/
Copy pathController.lua
More file actions
217 lines (195 loc) · 7.63 KB
/
Copy pathController.lua
File metadata and controls
217 lines (195 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
-- Controller profile: which paddle controller is in use (Xbox Elite, DualSense
-- Edge or anything else), detected from the active gamepad or chosen in the
-- settings. The profile only describes the controller; nothing here changes
-- paddle keys or actions.
local _, ns = ...
local Print = ns.Print
local SafeCall = ns.SafeCall
local FindRawInputDevice = ns.FindRawInputDevice
local PROFILES = {
xboxElite = { key = "xboxElite", label = "Xbox Elite", maxPaddles = 4 },
dualsenseEdge = { key = "dualsenseEdge", label = "DualSense Edge", maxPaddles = 2 },
custom = { key = "custom", label = "Generic", maxPaddles = 4 },
}
-- Order of the choices in the settings and /backhand controller.
local PROFILE_CHOICES = { "auto", "xboxElite", "dualsenseEdge", "custom" }
local VENDOR_MICROSOFT = 0x045E
local VENDOR_SONY = 0x054C
-- Product IDs differ by transport and firmware, so there is a list per
-- controller and the device name is checked as well. 0x02FF is what the
-- client's own Elite Series 2 config uses (README); Windows reports other
-- Xbox pads with it too, which is harmless because the generic profile has
-- the same four paddles. Steam Input presents every controller as an Xbox 360
-- pad (0x028E), so behind Steam only the manual choice can tell them apart.
local KNOWN_DEVICES = {
[VENDOR_MICROSOFT] = {
[0x02E3] = "xboxElite", -- Elite (first generation)
[0x02FF] = "xboxElite", -- client's Elite Series 2 config
[0x0B00] = "xboxElite", -- Elite Series 2, USB
[0x0B05] = "xboxElite", -- Elite Series 2, Bluetooth
[0x0B22] = "xboxElite", -- Elite Series 2, Bluetooth (newer firmware)
},
[VENDOR_SONY] = {
[0x0DF2] = "dualsenseEdge",
},
}
local function DetectProfileKey(device)
local byVendor = device.vendorID and KNOWN_DEVICES[device.vendorID]
local key = byVendor and device.productID and byVendor[device.productID]
if key then
return key
end
local name = type(device.name) == "string" and device.name:lower() or ""
if name:find("elite", 1, true) then
return "xboxElite"
end
if name:find("dualsense edge", 1, true) then
return "dualsenseEdge"
end
return "custom"
end
-- Reads the active (or first available) gamepad. Returns nil when no
-- controller is connected.
local function ReadDevice()
local deviceID, raw = FindRawInputDevice()
if not raw then
return nil
end
local mapped = C_GamePad and type(C_GamePad.GetDeviceMappedState) == "function"
and SafeCall(C_GamePad.GetDeviceMappedState, deviceID) or nil
return {
deviceID = deviceID,
name = raw.name,
vendorID = tonumber(raw.vendorID),
productID = tonumber(raw.productID),
labelStyle = type(mapped) == "table" and mapped.labelStyle or nil,
}
end
local function GetProfileSetting()
local setting = BackhandDB and BackhandDB.controllerProfile
if setting == "auto" or PROFILES[setting] then
return setting
end
return "auto"
end
-- The last controller seen. Kept while no controller is connected so the
-- profile does not flip back to generic every time the pad goes to sleep.
local lastDevice
-- Rebuilds ns.controllerProfile from the setting and the active controller.
local function RefreshControllerProfile()
local device = ReadDevice()
if device then
lastDevice = device
end
local known = device or lastDevice or {}
local setting = GetProfileSetting()
local detectedKey = DetectProfileKey(known)
local key = setting == "auto" and detectedKey or setting
local profile = PROFILES[key]
local previous = ns.controllerProfile
ns.controllerProfile = {
key = key,
label = profile.label,
maxPaddles = profile.maxPaddles,
detectedKey = detectedKey,
manual = setting ~= "auto",
connected = device ~= nil,
deviceID = known.deviceID,
name = known.name,
vendorID = known.vendorID,
productID = known.productID,
labelStyle = known.labelStyle,
}
-- The Settings button shows the mode as well as the profile ("Auto (Xbox
-- Elite)"), so switching between Auto and a manual setting that resolves
-- to the same profile must refresh it too.
local changed = not previous
or previous.key ~= key
or previous.manual ~= ns.controllerProfile.manual
or previous.detectedKey ~= detectedKey
if changed and ns.RefreshSettingsKeyRows then
ns.RefreshSettingsKeyRows()
end
return ns.controllerProfile
end
local function GetControllerProfile()
return ns.controllerProfile or RefreshControllerProfile()
end
local function GetProfileChoiceLabel(setting)
if setting == "auto" then
local profile = GetControllerProfile()
return "Auto (" .. PROFILES[profile.detectedKey].label .. ")"
end
return PROFILES[setting].label
end
local function SetControllerProfileSetting(setting)
BackhandDB.controllerProfile = setting
RefreshControllerProfile()
end
-- Steps the setting to the next choice, for the settings button.
local function CycleControllerProfileSetting()
local current = GetProfileSetting()
for index, setting in ipairs(PROFILE_CHOICES) do
if setting == current then
SetControllerProfileSetting(PROFILE_CHOICES[index % #PROFILE_CHOICES + 1])
return
end
end
SetControllerProfileSetting("auto")
end
local function FormatID(value)
if type(value) ~= "number" then
return "?"
end
return string.format("%d (0x%04X)", value, value)
end
local function GetControllerDiagnosticLines(separator)
local profile = RefreshControllerProfile()
local deviceText
if profile.vendorID then
deviceText = string.format("%s%sdevice %s%svendor %s%sproduct %s%slabelStyle=%s",
tostring(profile.name), separator,
tostring(profile.deviceID), separator,
FormatID(profile.vendorID), separator,
FormatID(profile.productID), separator,
tostring(profile.labelStyle))
if not profile.connected then
deviceText = deviceText .. " (last seen, none connected now)"
end
else
deviceText = "none connected"
end
return {
"Controller: " .. deviceText,
string.format("Controller profile: %s, %d paddles (%s)",
profile.label, profile.maxPaddles,
profile.manual and ("set manually; auto-detect says " .. PROFILES[profile.detectedKey].label) or "auto-detected"),
}
end
-- /backhand controller [auto|elite|edge|generic]
local PROFILE_ARGUMENTS = {
auto = "auto",
elite = "xboxElite", xbox = "xboxElite", xboxelite = "xboxElite",
edge = "dualsenseEdge", dualsense = "dualsenseEdge", dualsenseedge = "dualsenseEdge",
generic = "custom", custom = "custom",
}
local function HandleControllerCommand(argument)
if argument and argument ~= "" then
local setting = PROFILE_ARGUMENTS[argument:lower()]
if not setting then
Print("Usage: /backhand controller [auto|elite|edge|generic]")
return
end
SetControllerProfileSetting(setting)
end
local profile = RefreshControllerProfile()
Print(string.format("Controller profile: %s (%s).", profile.label,
profile.manual and "set manually" or "auto-detected"))
end
ns.RefreshControllerProfile = RefreshControllerProfile
ns.GetControllerProfile = GetControllerProfile
ns.GetProfileChoiceLabel = GetProfileChoiceLabel
ns.GetProfileSetting = GetProfileSetting
ns.CycleControllerProfileSetting = CycleControllerProfileSetting
ns.GetControllerDiagnosticLines = GetControllerDiagnosticLines
ns.HandleControllerCommand = HandleControllerCommand