Repository navigation
Expand file tree
/
Copy pathSteamController.h
More file actions
327 lines (278 loc) · 17.3 KB
/
Copy pathSteamController.h
File metadata and controls
327 lines (278 loc) · 17.3 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#pragma once
#include "hid/HidDevice.h"
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <mutex>
#include <thread>
class SteamController {
public:
static constexpr uint16_t VALVE_VID = 0x28DE;
static constexpr uint16_t SC2026_PID = 0x1302; // wired USB
static constexpr uint16_t SC2026_BT_PID = 0x1303; // Bluetooth LE (HID over GATT)
static constexpr uint16_t SC2026_DONGLE_PID = 0x1304; // wireless dongle ("Steam Controller Puck")
static constexpr uint16_t SC2026_NEREID_PID = 0x1305; // Nereid receiver
// How the controller is linked to the PC. Bluetooth is detected from the
// device interface path (HOGP service GUID / bthenum), the other two by PID.
enum class Transport { Unknown, Wired, Dongle, Bluetooth };
static Transport TransportFromPath(const std::wstring& path);
static const char* TransportName(Transport t);
// HID Usage Page for the vendor collection that carries all game input.
static constexpr uint16_t VENDOR_USAGE_PAGE = 0xFF00;
static constexpr uint16_t CONTROLLER_USAGE = 0x0001;
// Same page, different usage: a receiver's dock interface, which reports a
// controller set down on the puck. Carries no game input.
static constexpr uint16_t DOCK_USAGE = 0x0002;
enum class AccessClaim {
Failed,
Shared,
Exclusive,
};
// Input report IDs (device → host)
static constexpr uint8_t REPORT_STATE = 0x45; // BLE/no-quaternion state report
static constexpr uint8_t REPORT_STATE_LEGACY = 0x42; // USB/full state report (same layout)
static constexpr uint8_t REPORT_BATTERY_STATUS = 0x43; // TritonBatteryStatus_t
static constexpr uint8_t REPORT_STATUS = 0x44; // 5 bytes: battery / connection
static constexpr uint8_t REPORT_UNKNOWN_7B = 0x7B; // 12 bytes: TBD
static constexpr uint8_t REPORT_UNKNOWN_79 = 0x79; // 1 byte: TBD
static constexpr uint8_t CHARGE_STATE_DISCHARGING = 1;
static constexpr uint8_t CHARGE_STATE_CHARGING = 2;
static constexpr uint8_t CHARGE_STATE_CHARGING_DONE = 4;
// Feature report IDs — the command channel to the firmware.
// Commands are wrapped inside Feature Report 0x01 (or 0x02 as fallback).
// Buffer layout: [feature_report_id | cmd_byte | payload_size | payload...]
static constexpr uint8_t FEATURE_REPORT_CMD = 0x01;
static constexpr uint8_t FEATURE_REPORT_CMD2 = 0x02; // fallback if 0x01 fails
// Command bytes (go in buffer[1] inside the feature report)
static constexpr uint8_t CMD_SET_DIGITAL_MAPPINGS = 0x80;
static constexpr uint8_t CMD_CLEAR_DIGITAL_MAPPINGS = 0x81; // ← lizard off
static constexpr uint8_t CMD_GET_DIGITAL_MAPPINGS = 0x82;
static constexpr uint8_t CMD_SET_DEFAULT_MAPPINGS = 0x85; // ← lizard on
static constexpr uint8_t CMD_SET_SETTINGS = 0x87;
static constexpr uint8_t CMD_GET_SETTINGS = 0x89;
static constexpr uint8_t CMD_LOAD_DEFAULT_SETTINGS = 0x8E; // ← restore settings for lizard on
static constexpr uint8_t CMD_TURN_OFF_CONTROLLER = 0x9F; // payload "off!" — powers the pad down
// Triton output report IDs.
static constexpr uint8_t OUT_HAPTIC_RUMBLE = 0x80; // continuous two-channel haptic
static constexpr uint8_t OUT_HAPTIC_PULSE = 0x81; // one-shot pulse (on/off/repeat/gain)
static constexpr uint8_t OUT_HAPTIC_COMMAND = 0x82; // named command (tick/click + gain)
// Setting key IDs (go in the payload of CMD_SET_SETTINGS)
static constexpr uint8_t SETTING_RIGHT_TRACKPAD_MODE = 0x07;
static constexpr uint8_t SETTING_LEFT_TRACKPAD_MODE = 0x08;
static constexpr uint8_t SETTING_IMU_MODE = 0x30;
static constexpr uint8_t TRACKPAD_NONE = 0x00;
static constexpr uint16_t IMU_MODE_OFF = 0x0000;
static constexpr uint16_t IMU_MODE_RAW_ACCEL_GYRO = 0x0018;
// ---------------------------------------------------------------------------
// Input report layout — 0x45 / 0x42 STATE report (buf[0] = 0x45 or 0x42)
// ---------------------------------------------------------------------------
// buf[01] — 8-bit sequence counter (wraps 0xFF → 0x00)
// buf[02] — button bitmask byte 0
static constexpr uint8_t BTN_A = 0x01; // bit 0
static constexpr uint8_t BTN_B = 0x02; // bit 1
static constexpr uint8_t BTN_X = 0x04; // bit 2
static constexpr uint8_t BTN_Y = 0x08; // bit 3
static constexpr uint8_t BTN_QAM = 0x10; // bit 4 — "..." quick-access button (#121)
static constexpr uint8_t BTN_RS = 0x20; // bit 5 — right stick click
static constexpr uint8_t BTN_MENU = 0x40; // bit 6 — ≡ Menu / Start
static constexpr uint8_t BTN_R4 = 0x80; // bit 7 — back paddle R4
// buf[03] — button bitmask byte 1
static constexpr uint8_t BTN_R5 = 0x01; // bit 0 — back paddle R5
static constexpr uint8_t BTN_RB = 0x02; // bit 1
static constexpr uint8_t BTN_DPAD_DN = 0x04; // bit 2
static constexpr uint8_t BTN_DPAD_RT = 0x08; // bit 3
static constexpr uint8_t BTN_DPAD_LT = 0x10; // bit 4
static constexpr uint8_t BTN_DPAD_UP = 0x20; // bit 5
static constexpr uint8_t BTN_VIEW = 0x40; // bit 6 — ⧉ View / Back
static constexpr uint8_t BTN_LS = 0x80; // bit 7 — left stick click
// buf[04] — button bitmask byte 2
static constexpr uint8_t BTN_STEAM = 0x01; // bit 0 — Steam / Guide
static constexpr uint8_t BTN_L4 = 0x02; // bit 1 — back paddle L4
static constexpr uint8_t BTN_L5 = 0x04; // bit 2 — back paddle L5
static constexpr uint8_t BTN_LB = 0x08; // bit 3
static constexpr uint8_t BTN_RS_TOUCH = 0x10; // bit 4 — right stick capacitive touch
static constexpr uint8_t BTN_TP_RT = 0x20; // bit 5 — right trackpad active (touch or click)
static constexpr uint8_t BTN_TP_RT_CLICK = 0x40; // bit 6 — right trackpad hard press (physical click)
static constexpr uint8_t BTN_RT_FULL = 0x80; // bit 7 — right trigger fully pressed (digital threshold)
// buf[05] — flags byte
static constexpr uint8_t BTN_LS_TOUCH = 0x01; // bit 0 — left stick capacitive touch
static constexpr uint8_t BTN_TP_LT = 0x02; // bit 1 — left trackpad active (touch or click)
static constexpr uint8_t BTN_TP_LT_CLICK = 0x04; // bit 2 — left trackpad hard press
static constexpr uint8_t BTN_LT_FULL = 0x08; // bit 3 — left trigger fully pressed (digital threshold)
static constexpr uint8_t FLAG_GRIP_RT = 0x10; // bit 4 — right grip sensor active
static constexpr uint8_t FLAG_GRIP_LT = 0x20; // bit 5 — left grip sensor active
// other bits TBD
// buf[06..07] — left trigger, 16-bit LE signed, 0x0000 (released) – 0x7FFF (full)
// buf[08..09] — right trigger, 16-bit LE signed, 0x0000 (released) – 0x7FFF (full)
// buf[10..11] — left joystick X, 16-bit LE signed; center ≈ 0x0000, +0x7FFF = right, −0x8000 = left
// buf[12..13] — left joystick Y, 16-bit LE signed; center ≈ 0x0000, +0x7FFF = up, −0x8000 = down
// buf[14..15] — right joystick X, 16-bit LE signed; center ≈ 0x0000, +0x7FFF = right, −0x8000 = left
// buf[16..17] — right joystick Y, 16-bit LE signed; center ≈ 0x0000, +0x7FFF = up, −0x8000 = down
// buf[18..19] — left trackpad X, 16-bit LE signed (0x0000 when not touching)
// buf[20..21] — left trackpad Y, 16-bit LE signed (0x0000 when not touching)
// buf[22..23] — left trackpad contact area, 16-bit LE (0 = no contact; higher = more area/pressure)
// buf[24..25] — right trackpad X, 16-bit LE signed (0x0000 when not touching)
// buf[26..27] — right trackpad Y, 16-bit LE signed (0x0000 when not touching)
// buf[28..29] — right trackpad contact area, 16-bit LE (0 = no contact; higher = more area/pressure)
// IMU data — only present when IMU is enabled (SETTING_IMU_MODE = IMU_MODE_RAW_ACCEL_GYRO).
// Report length grows to ≥ 46 bytes.
// buf[30..33] — IMU timestamp, 32-bit LE unsigned (units ~16 µs)
// buf[34..35] — accelerometer X, 16-bit LE signed
// buf[36..37] — accelerometer Y, 16-bit LE signed
// buf[38..39] — accelerometer Z, 16-bit LE signed
// buf[40..41] — gyroscope X, 16-bit LE signed
// buf[42..43] — gyroscope Y, 16-bit LE signed
// buf[44..45] — gyroscope Z, 16-bit LE signed
// ---------------------------------------------------------------------------
SteamController() = default;
~SteamController() { Close(); }
SteamController(const SteamController&) = delete;
SteamController& operator=(const SteamController&) = delete;
// Returns paths for all live Steam Controller interfaces (probes each one).
static std::vector<std::wstring> EnumerateAll();
// Paths of every receiver's dock interface (DOCK_USAGE). Not a controller
// slot, so EnumerateAll leaves them out.
static std::vector<std::wstring> EnumerateDocks();
// Find and open the first live vendor HID interface.
bool Open();
// Open a specific path returned by EnumerateAll (no probe needed).
bool Open(const std::wstring& path);
// Adopt a handle the pounce thread already opened exclusively.
bool AdoptHandle(void* handle, const std::wstring& path);
void Close();
bool IsOpen() const { return m_device.IsOpen(); }
// Returns true for any report ID that carries controller state.
static bool IsStateReportId(uint8_t id) {
return id == REPORT_STATE || id == REPORT_STATE_LEGACY;
}
// Read until a live controller-state report arrives or the timeout expires.
// The puck exposes empty slot interfaces, and current firmware may reject
// feature commands until the active slot has produced an input report.
bool WaitForStateReport(uint32_t timeoutMs);
// Two-step sequence: clears digital mappings + sets trackpads to NONE.
// Starts the background rumble thread.
bool DisableLizardMode();
// Restores default mappings. Should be called before process exit.
bool EnableLizardMode();
// Prefer a write-exclusive handle, falling back to shared access when a
// compatible system handle is already open. Call before DisableLizardMode().
AccessClaim ClaimGameModeAccess();
// Reopen the device handle with full share flags, allowing other processes
// to open the device for write. Call after EnableLizardMode() when leaving
// game mode so Steam can reclaim the controller.
void ReleaseToShared();
// Read the next raw input report. buffer[0] = report ID on return.
// Returns 0 on timeout.
size_t ReadReport(uint8_t* buffer, size_t size, uint32_t timeoutMs = 16);
// Enable or disable raw IMU (accel + gyro) output in the state report.
bool SetImuEnabled(bool enabled);
// Set rumble intensity from XInput/DS4 motor bytes (0–255 each).
// Uses a power curve, cross-channel mixing, and a short attack boost on
// rising edges to produce a more natural feel from the haptic actuators.
// Call with (0, 0) to stop rumble.
void SetRumble(uint8_t largeMotor, uint8_t smallMotor);
// Re-assert the lizard-off state (cleared mappings + raw trackpad modes).
// The firmware silently reverts to lizard mode — including its autonomous
// click haptics — after a period without host feature reports. Call this
// every couple of seconds while game mode is active, like hid-steam does.
//
// `error` receives the Win32 error of the write that failed. It is taken
// while the write lock is still held: the rumble thread writes through the
// same device, and its next success would otherwise overwrite the evidence.
bool SendKeepalive(DWORD* error = nullptr);
// Why the last read failed outright, and how many reads were abandoned
// because the device stack would not complete the cancel. For the read loop
// to put in the event log; see HidDevice.
DWORD LastReadError() const { return m_device.LastReadError(); }
uint32_t AbandonedReads() const { return m_device.AbandonedReads(); }
// Ask the firmware to power the controller off — what Steam does for
// Steam + Y. Confirmed on a puck slot with payload "off!" (also accepted
// with no payload or a bare "off"). Only means anything on a wireless
// link: a pad on a USB cable is powered by the cable and stays on. Returns
// whether the write was accepted; the pad going quiet is the real answer.
bool PowerOff();
// Crash-context lizard restore: sends the default-mappings and
// default-settings feature reports WITHOUT taking locks or joining
// threads, so it is safe from an unhandled-exception filter where another
// thread may hold m_writeMutex forever. Racing a concurrent writer is
// acceptable — if this write is lost, the firmware's own revert timeout
// restores lizard mode a few seconds after the process dies.
void EmergencyLizardRestore() noexcept;
// Fire a single haptic event on one trackpad.
// strongClick = true → physical-click sensation; false → light touch-down tick.
void PulseTrackpadHaptic(bool left, bool strongClick);
// The simulated click for a dual-stage trigger (#75): a firmware click on
// the pad under the thumb on that trigger's side, plus a short buzz in the
// grip on the same side. There is no actuator in a trigger, so this comes
// from the controller body — which hardware testing found the most
// click-like of the candidates, with the pad click and the buzz together
// the strongest.
//
// Never writes to the device itself: it queues the click for the rumble
// thread and returns. This is called from the read loop, and a write that
// blocks (a Bluetooth link in trouble can hold one for seconds) must not
// stall the loop that is reading the trigger.
//
// Dropped rather than queued when the previous click on that side is still
// playing, for the reason TickTrackpadMovement is rate limited: the firmware
// plays the waveform to its end, and a backlog of them is a crunchy burst.
//
// The buzz joins the rumble a game is already asking for rather than
// replacing it — the higher of the two is what the grip plays, and the game's
// level resumes when the buzz ends. Sending an explicit zero when it ended,
// as the probe does, would cut an explosion short on every trigger pull.
void QueueTriggerClick(bool left);
// Fire a very light tick while the thumb moves on the trackpad.
// Returns false when the tick was dropped by the send rate limiter.
bool TickTrackpadMovement(bool left);
// Silence any in-flight trackpad haptic state (lifecycle cleanup hook).
void ClearTrackpadHaptics();
private:
struct RumbleFrame {
uint16_t left = 0;
uint16_t right = 0;
};
void RumbleLoop();
// Stops and joins the rumble thread if it is running, waking it from its
// wait first and discarding any trigger click it had not yet played.
void StopRumbleThread();
RumbleFrame CurrentRumbleFrameLocked(std::chrono::steady_clock::time_point now) const;
bool SendRumbleOutput(uint16_t leftSpeed, uint16_t rightSpeed);
bool SendTrackpadPulseOutput(uint8_t side, uint16_t onUs, uint16_t offUs,
uint16_t repeatCount, int16_t gainDb);
bool SendTrackpadCommandOutput(uint8_t side, uint8_t command, int8_t gainDb);
HidDevice m_device;
std::thread m_rumbleThread;
std::atomic<bool> m_running{false};
// Shared by the rumble thread and callers — protect with m_writeMutex.
std::mutex m_writeMutex;
// Rumble state — protect with m_rumbleMutex.
std::mutex m_rumbleMutex;
// Wakes RumbleLoop ahead of its 40 ms period, for a queued trigger click or
// for the moment a click's buzz is due to end. Waits on m_rumbleMutex.
std::condition_variable m_rumbleWake;
// One per trigger, indexed 0 = left, 1 = right. Protected by m_rumbleMutex.
struct TriggerClick {
bool pending = false; // pad click not yet sent by the rumble thread
std::chrono::steady_clock::time_point lastAt{}; // last accepted click
std::chrono::steady_clock::time_point buzzUntil{}; // grip buzz ends here
};
TriggerClick m_triggerClick[2];
uint16_t m_rumbleBaseLeft = 0;
uint16_t m_rumbleBaseRight = 0;
uint16_t m_rumbleBoostLeft = 0;
uint16_t m_rumbleBoostRight = 0;
std::chrono::steady_clock::time_point m_rumbleBoostUntil{};
std::chrono::steady_clock::time_point m_lastRumbleSent{};
uint8_t m_lastLargeMotor = 0;
uint8_t m_lastSmallMotor = 0;
bool m_hasRumbleState = false;
// Last trackpad haptic send per side (touched only by the read-loop
// thread, no lock needed). The firmware queues haptic waveforms — sending
// faster than they play builds a backlog that later drains as one crunchy
// burst. Movement ticks are dropped while the previous waveform is likely
// still playing; click haptics always send.
std::chrono::steady_clock::time_point m_lastTrackpadHapticLeft{};
std::chrono::steady_clock::time_point m_lastTrackpadHapticRight{};
};