Debian Codegraph @

Source/Core/InputCommon/ControllerInterface/SteamDeck/SteamDeck.cpp

raw symbols SCIP
1 // Copyright 2023 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "InputCommon/ControllerInterface/SteamDeck/SteamDeck.h"
5
6 #include <hidapi.h>
7
8 #include "Common/CommonTypes.h"
9 #include "Common/Logging/Log.h"
10 #include "Common/MathUtil.h"
11 #include "InputCommon/ControllerInterface/ControllerInterface.h"
12
13 namespace ciface::SteamDeck
14 {
15 constexpr std::string_view STEAMDECK_SOURCE_NAME = "SteamDeck";
16
17 struct DeckInputReport
18 {
19 // +0
20 u8 major_ver;
21 u8 minor_ver;
22 u8 report_type;
23 u8 report_sz;
24
25 u32 frame;
26
27 // +8
28 u32 buttons0;
29 u32 buttons1;
30
31 // +16
32 s16 l_pad_x;
33 s16 l_pad_y;
34 s16 r_pad_x;
35 s16 r_pad_y;
36
37 // +24
38 s16 accel_x;
39 s16 accel_y;
40 s16 accel_z;
41 s16 gyro_pitch;
42 s16 gyro_roll;
43 s16 gyro_yaw;
44 s16 pose_quat_w;
45 s16 pose_quat_x;
46 s16 pose_quat_y;
47 s16 pose_quat_z;
48
49 // +44
50 u16 l_trig;
51 u16 r_trig;
52
53 // +48
54 s16 l_stick_x;
55 s16 l_stick_y;
56 s16 r_stick_x;
57 s16 r_stick_y;
58
59 // +56
60 u16 l_pad_force;
61 u16 r_pad_force;
62 s16 l_stick_capa;
63 s16 r_stick_capa;
64 };
65 static_assert(sizeof(DeckInputReport) == 64);
66
67 class Device final : public Core::Device
68 {
69 private:
70 class Button final : public Input
71 {
72 public:
73 Button(const char* name, const u32& buttons, u32 mask)
74 : m_name(name), m_buttons(buttons), m_mask(mask)
75 {
76 }
77 std::string GetName() const override { return m_name; }
78 ControlState GetState() const override { return (m_buttons & m_mask) != 0; }
79
80 private:
81 const char* const m_name;
82 const u32& m_buttons;
83 const u32 m_mask;
84 };
85
86 template <class T>
87 class AnalogInput : public Input
88 {
89 public:
90 AnalogInput(const char* name, const T& input, ControlState range)
91 : m_name(name), m_input(input), m_range(range)
92 {
93 }
94 std::string GetName() const final { return m_name; }
95 ControlState GetState() const final { return ControlState(m_input) / m_range; }
96
97 private:
98 const char* m_name;
99 const T& m_input;
100 const ControlState m_range;
101 };
102
103 class MotionInput final : public AnalogInput<s16>
104 {
105 public:
106 using AnalogInput::AnalogInput;
107 bool IsDetectable() const override { return false; }
108 };
109
110 public:
111 Device(hid_device* device);
112 std::string GetName() const final override;
113 std::string GetSource() const final override;
114 Core::DeviceRemoval UpdateInput() override;
115
116 private:
117 hid_device* m_device;
118 DeckInputReport m_latest_input;
119 int m_gyro_reenable = 0;
120 };
121
122 class InputBackend final : public ciface::InputBackend
123 {
124 public:
125 InputBackend(ControllerInterface* controller_interface);
126 void PopulateDevices() override;
127 };
128
129 std::unique_ptr<ciface::InputBackend> CreateInputBackend(ControllerInterface* controller_interface)
130 {
131 return std::make_unique<InputBackend>(controller_interface);
132 }
133
134 InputBackend::InputBackend(ControllerInterface* controller_interface)
135 : ciface::InputBackend(controller_interface)
136 {
137 }
138
139 void InputBackend::PopulateDevices()
140 {
141 INFO_LOG_FMT(CONTROLLERINTERFACE, "SteamDeck PopulateDevices");
142
143 auto possible_devices = hid_enumerate(0x28de, 0x1205);
144 bool was_found = false;
145 std::string found_path;
146 auto this_device = possible_devices;
147
148 while (this_device)
149 {
150 INFO_LOG_FMT(CONTROLLERINTERFACE, "Found {} (interface {})", this_device->path,
151 this_device->interface_number);
152
153 if (this_device->interface_number == 2)
154 {
155 was_found = true;
156 found_path = this_device->path;
157 break;
158 }
159
160 this_device = this_device->next;
161 }
162
163 hid_free_enumeration(possible_devices);
164
165 if (was_found)
166 {
167 INFO_LOG_FMT(CONTROLLERINTERFACE, "Found Steam Deck controls at {}", found_path);
168 }
169 else
170 {
171 INFO_LOG_FMT(CONTROLLERINTERFACE, "No Steam Deck interface found");
172 return;
173 }
174
175 auto deck_dev = hid_open_path(found_path.c_str());
176 if (!deck_dev)
177 {
178 ERROR_LOG_FMT(CONTROLLERINTERFACE, "Steam Deck controls could not be opened");
179 return;
180 }
181 if (hid_set_nonblocking(deck_dev, 1) != 0)
182 {
183 ERROR_LOG_FMT(CONTROLLERINTERFACE, "Steam Deck controls could not be set nonblocking");
184 return;
185 }
186
187 GetControllerInterface().RemoveDevice(
188 [](const auto* dev) { return dev->GetSource() == STEAMDECK_SOURCE_NAME; });
189
190 GetControllerInterface().AddDevice(std::make_shared<Device>(deck_dev));
191 }
192
193 Device::Device(hid_device* device) : m_device{device}
194 {
195 // clang-format off
196 AddInput(new Button("R2 Full Pull", m_latest_input.buttons0, 0x00000001));
197 AddInput(new Button("L2 Full Pull", m_latest_input.buttons0, 0x00000002));
198 AddInput(new Button("R1", m_latest_input.buttons0, 0x00000004));
199 AddInput(new Button("L1", m_latest_input.buttons0, 0x00000008));
200 AddInput(new Button("Y", m_latest_input.buttons0, 0x00000010));
201 AddInput(new Button("B", m_latest_input.buttons0, 0x00000020));
202 AddInput(new Button("X", m_latest_input.buttons0, 0x00000040));
203 AddInput(new Button("A", m_latest_input.buttons0, 0x00000080));
204 AddInput(new Button("D-Pad Up", m_latest_input.buttons0, 0x00000100));
205 AddInput(new Button("D-Pad Right", m_latest_input.buttons0, 0x00000200));
206 AddInput(new Button("D-Pad Left", m_latest_input.buttons0, 0x00000400));
207 AddInput(new Button("D-Pad Down", m_latest_input.buttons0, 0x00000800));
208 AddInput(new Button("View", m_latest_input.buttons0, 0x00001000));
209 AddInput(new Button("Steam", m_latest_input.buttons0, 0x00002000));
210 AddInput(new Button("Menu", m_latest_input.buttons0, 0x00004000));
211 AddInput(new Button("L5", m_latest_input.buttons0, 0x00008000));
212 AddInput(new Button("R5", m_latest_input.buttons0, 0x00010000));
213 AddInput(new Button("Left Trackpad Click", m_latest_input.buttons0, 0x00020000));
214 AddInput(new Button("Right Trackpad Click", m_latest_input.buttons0, 0x00040000));
215 AddInput(new Button("Left Trackpad Touch", m_latest_input.buttons0, 0x00080000));
216 AddInput(new Button("Right Trackpad Touch", m_latest_input.buttons0, 0x00100000));
217 AddInput(new Button("L3", m_latest_input.buttons0, 0x00400000));
218 AddInput(new Button("R3", m_latest_input.buttons0, 0x04000000));
219 AddInput(new Button("L4", m_latest_input.buttons1, 0x00000200));
220 AddInput(new Button("R4", m_latest_input.buttons1, 0x00000400));
221 AddInput(new Button("Left Stick Touch", m_latest_input.buttons1, 0x00004000));
222 AddInput(new Button("Right Stick Touch", m_latest_input.buttons1, 0x00008000));
223 AddInput(new Button("Quick Access", m_latest_input.buttons1, 0x00040000));
224
225 AddInput(new AnalogInput("L2", m_latest_input.l_trig, 32767));
226 AddInput(new AnalogInput("R2", m_latest_input.r_trig, 32767));
227 AddInput(new AnalogInput("Left Stick X-", m_latest_input.l_stick_x, -32767));
228 AddInput(new AnalogInput("Left Stick X+", m_latest_input.l_stick_x, 32767));
229 AddInput(new AnalogInput("Left Stick Y-", m_latest_input.l_stick_y, -32767));
230 AddInput(new AnalogInput("Left Stick Y+", m_latest_input.l_stick_y, 32767));
231 AddInput(new AnalogInput("Right Stick X-", m_latest_input.r_stick_x, -32767));
232 AddInput(new AnalogInput("Right Stick X+", m_latest_input.r_stick_x, 32767));
233 AddInput(new AnalogInput("Right Stick Y-", m_latest_input.r_stick_y, -32767));
234 AddInput(new AnalogInput("Right Stick Y+", m_latest_input.r_stick_y, 32767));
235 AddInput(new AnalogInput("Left Trackpad X-", m_latest_input.l_pad_x, -32767));
236 AddInput(new AnalogInput("Left Trackpad X+", m_latest_input.l_pad_x, 32767));
237 AddInput(new AnalogInput("Left Trackpad Y-", m_latest_input.l_pad_y, -32767));
238 AddInput(new AnalogInput("Left Trackpad Y+", m_latest_input.l_pad_y, 32767));
239 AddInput(new AnalogInput("Right Trackpad X-", m_latest_input.r_pad_x, -32767));
240 AddInput(new AnalogInput("Right Trackpad X+", m_latest_input.r_pad_x, 32767));
241 AddInput(new AnalogInput("Right Trackpad Y-", m_latest_input.r_pad_y, -32767));
242 AddInput(new AnalogInput("Right Trackpad Y+", m_latest_input.r_pad_y, 32767));
243 AddInput(new AnalogInput("Left Trackpad Pressure", m_latest_input.l_pad_force, 32767));
244 AddInput(new AnalogInput("Right Trackpad Pressure", m_latest_input.r_pad_force, 32767));
245 AddInput(new AnalogInput("Left Stick Capacitance", m_latest_input.l_stick_capa, 32767));
246 AddInput(new AnalogInput("Right Stick Capacitance", m_latest_input.r_stick_capa, 32767));
247
248 // 0x4000 LSBs = 1 g
249 // final output in m/s^2
250 constexpr auto accel_scale = 16384.0 / MathUtil::GRAVITY_ACCELERATION;
251 AddInput(new MotionInput("Accel Up", m_latest_input.accel_z, -accel_scale));
252 AddInput(new MotionInput("Accel Down", m_latest_input.accel_z, accel_scale));
253 AddInput(new MotionInput("Accel Left", m_latest_input.accel_x, accel_scale));
254 AddInput(new MotionInput("Accel Right", m_latest_input.accel_x, -accel_scale));
255 AddInput(new MotionInput("Accel Forward", m_latest_input.accel_y, -accel_scale));
256 AddInput(new MotionInput("Accel Backward", m_latest_input.accel_y, accel_scale));
257
258 // 16.384 (?) LSBs = 1 deg / s
259 // final output in rads / s
260 constexpr auto gyro_scale = 16.384 * 360.0 / MathUtil::TAU;
261 AddInput(new MotionInput("Gyro Pitch Up", m_latest_input.gyro_pitch, gyro_scale));
262 AddInput(new MotionInput("Gyro Pitch Down", m_latest_input.gyro_pitch, -gyro_scale));
263 AddInput(new MotionInput("Gyro Roll Left", m_latest_input.gyro_roll, -gyro_scale));
264 AddInput(new MotionInput("Gyro Roll Right", m_latest_input.gyro_roll, gyro_scale));
265 AddInput(new MotionInput("Gyro Yaw Left", m_latest_input.gyro_yaw, gyro_scale));
266 AddInput(new MotionInput("Gyro Yaw Right", m_latest_input.gyro_yaw, -gyro_scale));
267 // clang-format on
268 }
269
270 std::string Device::GetName() const
271 {
272 return "Steam Deck";
273 }
274
275 std::string Device::GetSource() const
276 {
277 return std::string(STEAMDECK_SOURCE_NAME);
278 }
279
280 Core::DeviceRemoval Device::UpdateInput()
281 {
282 // As of a certain mid-2023 update to the Steam client,
283 // Steam will disable gyro data if gyro is not mapped in Steam Input.
284 // This disabling will happen every time the Steam overlay is closed.
285 // This command turns gyro data back on periodically.
286 if (++m_gyro_reenable == 250)
287 {
288 m_gyro_reenable = 0;
289 // Using names from Valve's contribution to SDL this packet decodes as:
290 // 0x00 = report ID
291 // 0x87 = ID_SET_SETTINGS_VALUES
292 // 0x03 = payload length
293 // 0x30 = SETTING_IMU_MODE
294 // 0x18 0x00 = SETTING_GYRO_MODE_SEND_RAW_ACCEL | SETTING_GYRO_MODE_SEND_RAW_GYRO
295 const unsigned char pkt[65] = {0x00, 0x87, 0x03, 0x30, 0x18, 0x00};
296 hid_send_feature_report(m_device, pkt, sizeof(pkt));
297 }
298
299 DeckInputReport rpt;
300 bool got_anything = false;
301 // Read all available input reports (processing only the most recent one).
302 while (hid_read(m_device, reinterpret_cast<u8*>(&rpt), sizeof(rpt)) > 0)
303 {
304 got_anything = true;
305 }
306 // In case there were no reports available to be read, bail early.
307 if (!got_anything)
308 return Core::DeviceRemoval::Keep;
309
310 if (rpt.major_ver != 0x01 || rpt.minor_ver != 0x00 || rpt.report_type != 0x09 ||
311 rpt.report_sz != sizeof(rpt))
312 {
313 ERROR_LOG_FMT(CONTROLLERINTERFACE, "Steam Deck bad report");
314 return Core::DeviceRemoval::Keep;
315 }
316
317 m_latest_input = rpt;
318
319 return Core::DeviceRemoval::Keep;
320 }
321
322 } // namespace ciface::SteamDeck