-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathreport.cpp
More file actions
1437 lines (1239 loc) · 51.4 KB
/
Copy pathreport.cpp
File metadata and controls
1437 lines (1239 loc) · 51.4 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file src/core/report.cpp
* @brief Gamepad report normalization and packing definitions.
*/
// standard includes
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <numbers>
#include <optional>
#include <span>
#include <utility>
// local includes
#include "shared/switch_pro_protocol.hpp"
#include <libvirtualhid/report.hpp>
namespace lvh::reports {
namespace {
constexpr std::uint8_t neutral_hat = 8;
using ByteReport = std::vector<std::byte>;
constexpr auto zero_byte = std::byte {0x00};
constexpr auto dualshock4_usb_output_report_id = std::byte {0x05};
constexpr auto dualshock4_bt_input_report_id = std::byte {0x11};
constexpr auto dualshock4_bt_input_hid_present = std::byte {0x80};
constexpr auto dualshock4_bt_output_report_id = std::byte {0x11};
constexpr auto dualshock4_output_hwctl_crc32 = std::byte {0x40};
constexpr auto dualshock4_flag0_rumble = std::byte {0x01};
constexpr auto dualshock4_flag0_lightbar = std::byte {0x02};
constexpr auto dualsense_usb_output_report_id = std::byte {0x02};
constexpr auto dualsense_bt_input_report_id = std::byte {0x31};
constexpr auto dualsense_bt_output_report_id = std::byte {0x31};
constexpr auto dualsense_bt_input_report_reserved = std::byte {0x00};
constexpr auto playstation_input_crc_seed = std::byte {0xA1};
constexpr auto playstation_output_crc_seed = std::byte {0xA2};
constexpr auto dualsense_flag0_rumble = std::byte {0x01};
constexpr auto dualsense_flag0_right_trigger = std::byte {0x04};
constexpr auto dualsense_flag0_left_trigger = std::byte {0x08};
constexpr auto dualsense_flag1_lightbar = std::byte {0x04};
constexpr auto dualsense_flag2_compatible_vibration = std::byte {0x04};
constexpr auto dualsense_acceleration_scale = 9.80665F * 100.0F;
constexpr auto dualsense_gyroscope_scale = 1145.0F * std::numbers::pi_v<float> / 180.0F;
constexpr std::uint8_t switch_rumble_and_subcommand_output_report_id = 0x01;
constexpr std::uint8_t switch_rumble_only_output_report_id = 0x10;
constexpr std::size_t switch_rumble_output_report_size = 10;
constexpr std::uint8_t switch_set_player_lights_subcommand = 0x30;
constexpr std::uint8_t switch_set_home_light_subcommand = 0x38;
constexpr float switch_acceleration_scale = 4096.0F / 9.80665F;
constexpr float switch_gyroscope_scale = 14.2842F;
std::uint8_t decode_switch_home_light_intensity(std::byte encoded_intensity) {
const auto intensity = std::to_integer<std::uint8_t>(encoded_intensity >> 4U);
if (intensity == 0U) {
return 0U;
}
if (intensity <= 6U) {
return static_cast<std::uint8_t>(std::lround(static_cast<float>(intensity) * 25.5F));
}
if (intensity == 15U) {
return 255U;
}
const auto normalized = (static_cast<float>(intensity) - 0.5F) / 15.0F;
return static_cast<std::uint8_t>(std::lround(std::pow(normalized, 1.0F / 2.13F) * 255.0F));
}
// SDL maps 16-bit rumble strengths to Nintendo's shared 101-step amplitude
// scale. This is the inverse table for the packed high- and low-band values:
// https://github.com/libsdl-org/SDL/blob/main/src/joystick/hidapi/SDL_hidapi_switch.c
constexpr std::array<std::uint16_t, 101> switch_rumble_amplitudes {
0,
514,
775,
921,
1096,
1303,
1550,
1843,
2192,
2606,
3100,
3686,
4383,
5213,
6199,
7372,
7698,
8039,
8395,
8767,
9155,
9560,
9984,
10426,
10887,
11369,
11873,
12398,
12947,
13520,
14119,
14744,
15067,
15397,
15734,
16079,
16431,
16790,
17158,
17534,
17918,
18310,
18711,
19121,
19540,
19967,
20405,
20851,
21308,
21775,
22251,
22739,
23236,
23745,
24265,
24797,
25340,
25894,
26462,
27041,
27633,
28238,
28856,
29488,
30134,
30794,
31468,
32157,
32861,
33581,
34316,
35068,
35836,
36620,
37422,
38242,
39079,
39935,
40809,
41703,
42616,
43549,
44503,
45477,
46473,
47491,
48531,
49593,
50679,
51789,
52923,
54082,
55266,
56476,
57713,
58977,
60268,
61588,
62936,
64315,
65535,
};
constexpr std::byte to_byte(std::uint8_t value) {
return static_cast<std::byte>(value);
}
constexpr std::byte to_low_byte(std::uint32_t value) {
return static_cast<std::byte>(value & 0xFFU);
}
constexpr std::uint8_t to_uint8(std::byte value) {
return std::to_integer<std::uint8_t>(value);
}
bool has_flag(std::byte value, std::byte flag) {
return (value & flag) != zero_byte;
}
void add_flag(ByteReport &report, std::size_t offset, std::byte flag) {
report[offset] |= flag;
}
std::vector<std::uint8_t> to_uint8_report(const ByteReport &report) {
std::vector<std::uint8_t> bytes;
bytes.reserve(report.size());
for (const auto value : report) {
bytes.push_back(to_uint8(value));
}
return bytes;
}
ByteReport to_byte_report(const std::vector<std::uint8_t> &report) {
ByteReport bytes;
bytes.reserve(report.size());
for (const auto value : report) {
bytes.push_back(to_byte(value));
}
return bytes;
}
void write_u16(ByteReport &report, std::size_t offset, std::uint16_t value) {
report[offset] = to_low_byte(value);
report[offset + 1U] = to_low_byte(value >> 8U);
}
void write_u32(ByteReport &report, std::size_t offset, std::uint32_t value) {
report[offset] = to_low_byte(value);
report[offset + 1U] = to_low_byte(value >> 8U);
report[offset + 2U] = to_low_byte(value >> 16U);
report[offset + 3U] = to_low_byte(value >> 24U);
}
void write_i16(ByteReport &report, std::size_t offset, std::int16_t value) {
write_u16(report, offset, static_cast<std::uint16_t>(value));
}
std::uint16_t read_u16(const std::vector<std::uint8_t> &report, std::size_t offset) {
const auto low = std::to_integer<std::uint16_t>(to_byte(report[offset]));
const auto high = std::to_integer<std::uint16_t>(to_byte(report[offset + 1U]));
return static_cast<std::uint16_t>(low | static_cast<std::uint16_t>(high << 8U));
}
void append_u16(std::vector<std::uint8_t> &report, std::uint16_t value) {
report.push_back(static_cast<std::uint8_t>(value & 0xFFU));
report.push_back(static_cast<std::uint8_t>((value >> 8U) & 0xFFU));
}
std::uint32_t read_u32(const ByteReport &report, std::size_t offset) {
return std::to_integer<std::uint32_t>(report[offset]) |
(std::to_integer<std::uint32_t>(report[offset + 1U]) << 8U) |
(std::to_integer<std::uint32_t>(report[offset + 2U]) << 16U) |
(std::to_integer<std::uint32_t>(report[offset + 3U]) << 24U);
}
std::uint32_t crc32(std::span<const std::byte> buffer, std::uint32_t seed = 0) {
auto crc = seed ^ 0xFFFFFFFFU;
for (const auto value : buffer) {
crc ^= std::to_integer<std::uint32_t>(value);
for (auto bit = 0; bit < 8; ++bit) {
const auto mask = 0U - (crc & 1U);
crc = (crc >> 1U) ^ (0xEDB88320U & mask);
}
}
return crc ^ 0xFFFFFFFFU;
}
std::uint32_t playstation_crc_seed(std::byte seed) {
const std::array seed_report {seed};
return crc32(seed_report);
}
void write_playstation_crc(ByteReport &report, std::byte seed) {
if (report.size() < 4U) {
return;
}
const auto crc_offset = report.size() - 4U;
write_u32(report, crc_offset, crc32({report.data(), crc_offset}, playstation_crc_seed(seed)));
}
std::int16_t scale_i16(float value, float multiplier) {
const auto scaled = std::clamp(value * multiplier, -32768.0F, 32767.0F);
return static_cast<std::int16_t>(std::lround(scaled));
}
std::uint16_t normalize_unsigned_axis(float value) {
return static_cast<std::uint16_t>(std::lround((clamp_axis(value) + 1.0F) * 32767.5F));
}
std::uint8_t normalize_u8_axis(float value) {
return static_cast<std::uint8_t>(std::lround((clamp_axis(value) + 1.0F) * 127.5F));
}
std::uint16_t scale_output_byte(std::uint8_t value) {
return static_cast<std::uint16_t>(std::lround((static_cast<float>(value) / 255.0F) * 65535.0F));
}
constexpr std::size_t pid_rumble_payload_size = 8;
constexpr std::size_t pid_rumble_report_size = pid_rumble_payload_size + 1U;
constexpr std::uint8_t pid_rumble_maximum_magnitude = 100;
constexpr std::uint8_t pid_rumble_low_frequency_enabled = 0x02;
constexpr std::uint8_t pid_rumble_high_frequency_enabled = 0x01;
constexpr std::uint8_t pid_rumble_left_trigger_enabled = 0x08;
constexpr std::uint8_t pid_rumble_right_trigger_enabled = 0x04;
struct PidRumbleAmplitude {
std::uint16_t left_trigger;
std::uint16_t right_trigger;
std::uint16_t low_frequency;
std::uint16_t high_frequency;
};
std::uint16_t scale_pid_rumble_magnitude(std::uint8_t value) {
return static_cast<std::uint16_t>(
(static_cast<std::uint32_t>(value) * 65535U + 50U) / pid_rumble_maximum_magnitude
);
}
std::optional<std::size_t> pid_rumble_payload_offset(
const DeviceProfile &profile,
const std::vector<std::uint8_t> &report
) {
using enum GamepadProfileKind;
if (profile.gamepad_kind == generic) {
if (report.size() >= pid_rumble_report_size && report[0] == profile.report_id) {
return 1U;
}
return std::nullopt;
}
if (profile.gamepad_kind != xbox_one && profile.gamepad_kind != xbox_series) {
return std::nullopt;
}
// Some native Xbox Series transports use report ID 3 followed by the
// same eight-byte four-motor rumble payload.
if (profile.gamepad_kind == xbox_series && report.size() >= pid_rumble_report_size && report[0] == 0x03U) {
return 1U;
}
// The Windows HID write buffer includes a leading zero for devices that
// do not use report IDs. VHF may preserve that byte or expose only the
// eight-byte PID payload, so accept both representations.
if (report.size() >= pid_rumble_report_size && report[0] == 0U) {
return 1U;
}
if (report.size() >= pid_rumble_payload_size) {
return 0U;
}
return std::nullopt;
}
std::optional<PidRumbleAmplitude> decode_pid_rumble_report(
const DeviceProfile &profile,
const std::vector<std::uint8_t> &report
) {
const auto offset = pid_rumble_payload_offset(profile, report);
if (!offset.has_value()) {
return std::nullopt;
}
const auto magnitudes = std::span {report}.subspan(*offset + 1U, 4U);
if (std::ranges::any_of(magnitudes, [](const auto magnitude) {
return magnitude > pid_rumble_maximum_magnitude;
})) {
return std::nullopt;
}
const auto enabled = std::byte {report[*offset]} & std::byte {0x0F};
const auto duration = report[*offset + 5U];
const auto enabled_magnitude = [duration, enabled, magnitudes](std::uint8_t enable_bit, std::size_t magnitude_index) {
if (duration == 0U || (enabled & std::byte {enable_bit}) == zero_byte) {
return std::uint16_t {0};
}
return scale_pid_rumble_magnitude(magnitudes[magnitude_index]);
};
return PidRumbleAmplitude {
.left_trigger = enabled_magnitude(pid_rumble_left_trigger_enabled, 0U),
.right_trigger = enabled_magnitude(pid_rumble_right_trigger_enabled, 1U),
.low_frequency = enabled_magnitude(pid_rumble_low_frequency_enabled, 2U),
.high_frequency = enabled_magnitude(pid_rumble_high_frequency_enabled, 3U),
};
}
struct SwitchRumbleAmplitude {
std::uint16_t low_frequency;
std::uint16_t high_frequency;
};
std::optional<SwitchRumbleAmplitude> decode_switch_rumble_frame(
const std::vector<std::uint8_t> &report,
std::size_t offset
) {
const auto high_frequency_code = std::byte {report[offset + 1U]} & std::byte {0xFE};
const auto high_frequency_index = std::to_integer<std::size_t>(high_frequency_code) / 2U;
const auto low_frequency_code = std::byte {report[offset + 3U]};
const auto low_frequency_value = std::to_integer<std::uint8_t>(low_frequency_code);
if (high_frequency_index >= switch_rumble_amplitudes.size() || low_frequency_value < 0x40U || low_frequency_value > 0x72U) {
return std::nullopt;
}
const auto low_frequency_high_bit = std::byte {report[offset + 2U]} & std::byte {0x80};
const auto low_frequency_index = static_cast<std::size_t>((low_frequency_value - 0x40U) * 2U) +
(low_frequency_high_bit != zero_byte ? 1U : 0U);
if (low_frequency_index >= switch_rumble_amplitudes.size()) {
return std::nullopt;
}
return SwitchRumbleAmplitude {
.low_frequency = switch_rumble_amplitudes[low_frequency_index],
.high_frequency = switch_rumble_amplitudes[high_frequency_index],
};
}
std::optional<SwitchRumbleAmplitude> decode_switch_rumble_report(
const std::vector<std::uint8_t> &report
) {
if (report.size() < switch_rumble_output_report_size || (report[0] != switch_rumble_and_subcommand_output_report_id && report[0] != switch_rumble_only_output_report_id)) {
return std::nullopt;
}
const auto left = decode_switch_rumble_frame(report, 2U);
const auto right = decode_switch_rumble_frame(report, 6U);
if (!left.has_value() || !right.has_value()) {
return std::nullopt;
}
// The normalized API has one strength per frequency band, while the
// native report has one frame per actuator. Retain the strongest request
// in each band so asymmetric native effects are not lost entirely.
return SwitchRumbleAmplitude {
.low_frequency = std::max(left->low_frequency, right->low_frequency),
.high_frequency = std::max(left->high_frequency, right->high_frequency),
};
}
struct ButtonBit {
std::uint16_t bit;
GamepadButton button;
};
constexpr auto face_shoulder_button_map() {
using enum GamepadButton;
return std::array {
ButtonBit {0U, a},
ButtonBit {1U, b},
ButtonBit {2U, x},
ButtonBit {3U, y},
ButtonBit {4U, left_shoulder},
ButtonBit {5U, right_shoulder},
};
}
constexpr auto common_menu_button_map() {
using enum GamepadButton;
return std::array {
ButtonBit {6U, back},
ButtonBit {7U, start},
ButtonBit {8U, left_stick},
ButtonBit {9U, right_stick},
};
}
constexpr auto common_extra_button_map() {
using enum GamepadButton;
return std::array {
ButtonBit {10U, guide},
ButtonBit {11U, misc1},
};
}
constexpr auto standard_dpad_button_map() {
using enum GamepadButton;
return std::array {
ButtonBit {12U, dpad_up},
ButtonBit {13U, dpad_down},
ButtonBit {14U, dpad_left},
ButtonBit {15U, dpad_right},
};
}
constexpr auto xbox_extra_button_map() {
using enum GamepadButton;
return std::array {
ButtonBit {11U, misc1},
};
}
std::uint16_t button_bits(std::span<const ButtonBit> button_map, const ButtonSet &buttons) {
auto bits = std::uint16_t {};
for (const auto [bit, button] : button_map) {
if (buttons.test(button)) {
bits |= static_cast<std::uint16_t>(1U << bit);
}
}
return bits;
}
std::uint16_t common_button_bits(const ButtonSet &buttons) {
return static_cast<std::uint16_t>(
button_bits(face_shoulder_button_map(), buttons) |
button_bits(common_menu_button_map(), buttons) |
button_bits(common_extra_button_map(), buttons)
);
}
std::uint16_t standard_gamepad_button_bits(const ButtonSet &buttons) {
return static_cast<std::uint16_t>(
common_button_bits(buttons) |
button_bits(standard_dpad_button_map(), buttons)
);
}
std::uint16_t xbox_gip_button_bits(const ButtonSet &buttons) {
return static_cast<std::uint16_t>(
button_bits(face_shoulder_button_map(), buttons) |
button_bits(common_menu_button_map(), buttons) |
button_bits(xbox_extra_button_map(), buttons)
);
}
std::byte dualsense_battery_state(GamepadBatteryState state) {
switch (state) {
using enum GamepadBatteryState;
case discharging:
return std::byte {0x00};
case charging:
return std::byte {0x01};
case full:
return std::byte {0x02};
case voltage_or_temperature_error:
return std::byte {0x0A};
case temperature_error:
return std::byte {0x0B};
case charging_error:
return std::byte {0x0F};
case unknown:
break;
}
return std::byte {0x02};
}
void write_dualsense_touch_contact(
ByteReport &report,
std::size_t offset,
const GamepadTouchContact &contact
) {
const auto x = static_cast<std::uint16_t>(std::lround(std::clamp(contact.x, 0.0F, 1.0F) * 1919.0F));
const auto y = static_cast<std::uint16_t>(std::lround(std::clamp(contact.y, 0.0F, 1.0F) * 1079.0F));
report[offset] = (to_byte(contact.id) & std::byte {0x7F}) | (contact.active ? zero_byte : std::byte {0x80});
report[offset + 1U] = to_low_byte(x);
report[offset + 2U] = to_low_byte(((x >> 8U) & 0x0FU) | ((y & 0x0FU) << 4U));
report[offset + 3U] = to_low_byte(y >> 4U);
}
void write_dualshock4_touch_contact(
ByteReport &report,
std::size_t offset,
const GamepadTouchContact &contact
) {
const auto x = static_cast<std::uint16_t>(std::lround(std::clamp(contact.x, 0.0F, 1.0F) * 1919.0F));
const auto y = static_cast<std::uint16_t>(std::lround(std::clamp(contact.y, 0.0F, 1.0F) * 941.0F));
report[offset] = (to_byte(contact.id) & std::byte {0x7F}) | (contact.active ? zero_byte : std::byte {0x80});
report[offset + 1U] = to_low_byte(x);
report[offset + 2U] = to_low_byte(((x >> 8U) & 0x0FU) | ((y & 0x0FU) << 4U));
report[offset + 3U] = to_low_byte(y >> 4U);
}
std::uint8_t dualshock4_battery_status(const GamepadBattery &battery) {
using enum GamepadBatteryState;
if (battery.state == full) {
return 0x1B;
}
if (
battery.state == voltage_or_temperature_error ||
battery.state == temperature_error ||
battery.state == charging_error
) {
return 0x0F;
}
const auto charge = std::min<std::uint8_t>(10U, static_cast<std::uint8_t>(std::lround(battery.percentage / 10.0F)));
if (battery.state == discharging) {
return charge;
}
return static_cast<std::uint8_t>(0x10U | charge);
}
std::uint8_t dualshock4_battery_level(const GamepadBattery &battery) {
if (battery.state == GamepadBatteryState::full && battery.percentage >= 100U) {
return 0xFF;
}
return static_cast<std::uint8_t>(std::lround((static_cast<float>(battery.percentage) / 100.0F) * 255.0F));
}
std::uint16_t dualshock4_sensor_timestamp() {
static const auto start = std::chrono::steady_clock::now();
const auto elapsed =
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count();
return static_cast<std::uint16_t>((static_cast<std::uint64_t>(elapsed) * 3U) / 16U);
}
std::uint8_t dualsense_sequence_number() {
static std::atomic_uint32_t sequence_number = 0;
return static_cast<std::uint8_t>((sequence_number.fetch_add(1U, std::memory_order_relaxed) + 1U) % 255U);
}
std::uint32_t dualsense_sensor_timestamp() {
const auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch()
)
.count();
return static_cast<std::uint32_t>(static_cast<std::uint64_t>(elapsed) / 333U);
}
std::vector<std::uint8_t> pack_dualshock4_input_report(const DeviceProfile &profile, const GamepadState &state) {
const auto is_bluetooth = profile.bus_type == BusType::bluetooth;
const auto payload_offset = is_bluetooth ? 3U : 1U;
if (const auto minimum_report_size = is_bluetooth ? 78U : 64U; profile.input_report_size < minimum_report_size) {
return {};
}
const auto normalized = normalize_state(state);
const auto acceleration = normalized.acceleration.value_or(Vector3 {.x = 0.0F, .y = 9.80665F, .z = 0.0F});
const auto gyroscope = normalized.gyroscope.value_or(Vector3 {});
const auto battery = normalized.battery.value_or(GamepadBattery {.state = GamepadBatteryState::full, .percentage = 100});
ByteReport report(profile.input_report_size, zero_byte);
report[0] = is_bluetooth ? dualshock4_bt_input_report_id : to_byte(profile.report_id);
if (is_bluetooth) {
report[1] = dualshock4_bt_input_hid_present;
}
report[payload_offset + 0U] = to_byte(normalize_u8_axis(normalized.left_stick.x));
report[payload_offset + 1U] = to_byte(normalize_u8_axis(-normalized.left_stick.y));
report[payload_offset + 2U] = to_byte(normalize_u8_axis(normalized.right_stick.x));
report[payload_offset + 3U] = to_byte(normalize_u8_axis(-normalized.right_stick.y));
report[payload_offset + 4U] = to_byte(hat_from_buttons(normalized.buttons));
if (normalized.buttons.test(GamepadButton::x)) {
add_flag(report, payload_offset + 4U, std::byte {0x10});
}
if (normalized.buttons.test(GamepadButton::a)) {
add_flag(report, payload_offset + 4U, std::byte {0x20});
}
if (normalized.buttons.test(GamepadButton::b)) {
add_flag(report, payload_offset + 4U, std::byte {0x40});
}
if (normalized.buttons.test(GamepadButton::y)) {
add_flag(report, payload_offset + 4U, std::byte {0x80});
}
if (normalized.buttons.test(GamepadButton::left_shoulder)) {
add_flag(report, payload_offset + 5U, std::byte {0x01});
}
if (normalized.buttons.test(GamepadButton::right_shoulder)) {
add_flag(report, payload_offset + 5U, std::byte {0x02});
}
if (normalized.left_trigger > 0.0F) {
add_flag(report, payload_offset + 5U, std::byte {0x04});
}
if (normalized.right_trigger > 0.0F) {
add_flag(report, payload_offset + 5U, std::byte {0x08});
}
if (normalized.buttons.test(GamepadButton::back)) {
add_flag(report, payload_offset + 5U, std::byte {0x10});
}
if (normalized.buttons.test(GamepadButton::start)) {
add_flag(report, payload_offset + 5U, std::byte {0x20});
}
if (normalized.buttons.test(GamepadButton::left_stick)) {
add_flag(report, payload_offset + 5U, std::byte {0x40});
}
if (normalized.buttons.test(GamepadButton::right_stick)) {
add_flag(report, payload_offset + 5U, std::byte {0x80});
}
if (normalized.buttons.test(GamepadButton::guide)) {
add_flag(report, payload_offset + 6U, std::byte {0x01});
}
if (normalized.buttons.test(GamepadButton::touchpad)) {
add_flag(report, payload_offset + 6U, std::byte {0x02});
}
report[payload_offset + 7U] = to_byte(normalize_trigger(normalized.left_trigger));
report[payload_offset + 8U] = to_byte(normalize_trigger(normalized.right_trigger));
write_u16(report, payload_offset + 9U, dualshock4_sensor_timestamp());
report[payload_offset + 11U] = to_byte(dualshock4_battery_level(battery));
write_i16(report, payload_offset + 12U, scale_i16(gyroscope.x, 20.0F));
write_i16(report, payload_offset + 14U, scale_i16(gyroscope.y, 20.0F));
write_i16(report, payload_offset + 16U, scale_i16(gyroscope.z, 20.0F));
write_i16(report, payload_offset + 18U, scale_i16(acceleration.x, 10000.0F / 9.80665F));
write_i16(report, payload_offset + 20U, scale_i16(acceleration.y, 10000.0F / 9.80665F));
write_i16(report, payload_offset + 22U, scale_i16(acceleration.z, 10000.0F / 9.80665F));
report[payload_offset + 29U] = to_byte(dualshock4_battery_status(battery));
const auto touch_report_offset = payload_offset + 33U;
report[payload_offset + 32U] = std::byte {0x01};
write_dualshock4_touch_contact(report, touch_report_offset + 1U, normalized.touchpad_contacts[0]);
write_dualshock4_touch_contact(report, touch_report_offset + 5U, normalized.touchpad_contacts[1]);
if (is_bluetooth) {
write_playstation_crc(report, playstation_input_crc_seed);
}
return to_uint8_report(report);
}
std::vector<std::uint8_t> pack_dualsense_input_report(const DeviceProfile &profile, const GamepadState &state) {
const auto is_bluetooth = profile.bus_type == BusType::bluetooth;
const auto payload_offset = is_bluetooth ? 2U : 1U;
if (const auto minimum_report_size = is_bluetooth ? 78U : 64U; profile.input_report_size < minimum_report_size) {
return {};
}
const auto normalized = normalize_state(state);
ByteReport report(profile.input_report_size, zero_byte);
report[0] = is_bluetooth ? dualsense_bt_input_report_id : to_byte(profile.report_id);
if (is_bluetooth) {
report[1] = dualsense_bt_input_report_reserved;
}
report[payload_offset + 0U] = to_byte(normalize_u8_axis(normalized.left_stick.x));
report[payload_offset + 1U] = to_byte(normalize_u8_axis(-normalized.left_stick.y));
report[payload_offset + 2U] = to_byte(normalize_u8_axis(normalized.right_stick.x));
report[payload_offset + 3U] = to_byte(normalize_u8_axis(-normalized.right_stick.y));
report[payload_offset + 4U] = to_byte(normalize_trigger(normalized.left_trigger));
report[payload_offset + 5U] = to_byte(normalize_trigger(normalized.right_trigger));
report[payload_offset + 6U] = to_byte(dualsense_sequence_number());
report[payload_offset + 7U] = to_byte(hat_from_buttons(normalized.buttons));
if (normalized.buttons.test(GamepadButton::x)) {
add_flag(report, payload_offset + 7U, std::byte {0x10});
}
if (normalized.buttons.test(GamepadButton::a)) {
add_flag(report, payload_offset + 7U, std::byte {0x20});
}
if (normalized.buttons.test(GamepadButton::b)) {
add_flag(report, payload_offset + 7U, std::byte {0x40});
}
if (normalized.buttons.test(GamepadButton::y)) {
add_flag(report, payload_offset + 7U, std::byte {0x80});
}
if (normalized.buttons.test(GamepadButton::left_shoulder)) {
add_flag(report, payload_offset + 8U, std::byte {0x01});
}
if (normalized.buttons.test(GamepadButton::right_shoulder)) {
add_flag(report, payload_offset + 8U, std::byte {0x02});
}
if (normalized.left_trigger > 0.0F) {
add_flag(report, payload_offset + 8U, std::byte {0x04});
}
if (normalized.right_trigger > 0.0F) {
add_flag(report, payload_offset + 8U, std::byte {0x08});
}
if (normalized.buttons.test(GamepadButton::back)) {
add_flag(report, payload_offset + 8U, std::byte {0x10});
}
if (normalized.buttons.test(GamepadButton::start)) {
add_flag(report, payload_offset + 8U, std::byte {0x20});
}
if (normalized.buttons.test(GamepadButton::left_stick)) {
add_flag(report, payload_offset + 8U, std::byte {0x40});
}
if (normalized.buttons.test(GamepadButton::right_stick)) {
add_flag(report, payload_offset + 8U, std::byte {0x80});
}
if (normalized.buttons.test(GamepadButton::guide)) {
add_flag(report, payload_offset + 9U, std::byte {0x01});
}
if (normalized.buttons.test(GamepadButton::touchpad)) {
add_flag(report, payload_offset + 9U, std::byte {0x02});
}
if (normalized.buttons.test(GamepadButton::misc1)) {
add_flag(report, payload_offset + 9U, std::byte {0x04});
}
if (normalized.gyroscope) {
write_i16(report, payload_offset + 15U, scale_i16(normalized.gyroscope->x, dualsense_gyroscope_scale));
write_i16(report, payload_offset + 17U, scale_i16(normalized.gyroscope->y, dualsense_gyroscope_scale));
write_i16(report, payload_offset + 19U, scale_i16(normalized.gyroscope->z, dualsense_gyroscope_scale));
}
if (normalized.acceleration) {
write_i16(report, payload_offset + 21U, scale_i16(normalized.acceleration->x, dualsense_acceleration_scale));
write_i16(report, payload_offset + 23U, scale_i16(normalized.acceleration->y, dualsense_acceleration_scale));
write_i16(report, payload_offset + 25U, scale_i16(normalized.acceleration->z, dualsense_acceleration_scale));
}
write_u32(report, payload_offset + 27U, dualsense_sensor_timestamp());
write_dualsense_touch_contact(report, payload_offset + 32U, normalized.touchpad_contacts[0]);
write_dualsense_touch_contact(report, payload_offset + 36U, normalized.touchpad_contacts[1]);
const auto battery = normalized.battery.value_or(GamepadBattery {.state = GamepadBatteryState::full, .percentage = 100});
const auto battery_charge = std::min<std::uint8_t>(10U, static_cast<std::uint8_t>(std::lround(battery.percentage / 10.0F)));
report[payload_offset + 52U] =
to_byte(battery_charge) | (dualsense_battery_state(battery.state) << 4U);
report[payload_offset + 53U] = std::byte {0x0C};
if (is_bluetooth) {
write_playstation_crc(report, playstation_input_crc_seed);
}
return to_uint8_report(report);
}
std::optional<std::size_t> dualsense_common_output_offset(const ByteReport &report) {
if (report.size() >= 48U && report[0] == dualsense_usb_output_report_id) {
return 1U;
}
if (report.size() >= 49U && report[0] == dualsense_bt_output_report_id) {
if (report.size() >= 78U) {
const auto expected_crc = crc32({report.data(), report.size() - 4U}, playstation_crc_seed(playstation_output_crc_seed));
const auto actual_crc = read_u32(report, report.size() - 4U);
if (actual_crc != expected_crc) {
return std::nullopt;
}
}
const auto enable_hid = has_flag(report[1], std::byte {0x02});
if (!enable_hid && report.size() < 50U) {
return std::nullopt;
}
return enable_hid ? 2U : 3U;
}
return std::nullopt;
}
std::optional<std::size_t> dualshock4_common_output_offset(const ByteReport &report) {
if (report.size() >= 32U && report[0] == dualshock4_usb_output_report_id) {
return 1U;
}
if (report.size() >= 78U && report[0] == dualshock4_bt_output_report_id) {
if (has_flag(report[1], dualshock4_output_hwctl_crc32)) {
const auto expected_crc = crc32({report.data(), report.size() - 4U}, playstation_crc_seed(playstation_output_crc_seed));
const auto actual_crc = read_u32(report, report.size() - 4U);
if (actual_crc != expected_crc) {
return std::nullopt;
}
}
return 3U;
}
return std::nullopt;
}
void append_dualshock4_outputs(
const ByteReport &report,
const std::vector<std::uint8_t> &raw_report,
std::size_t offset,
std::vector<GamepadOutput> &outputs
) {
const auto valid_flag0 = report[offset];
const auto valid_flag1 = report[offset + 1U];
const auto motor_right = raw_report[offset + 3U];
const auto motor_left = raw_report[offset + 4U];
if (has_flag(valid_flag0, dualshock4_flag0_rumble)) {
GamepadOutput output;
output.kind = GamepadOutputKind::rumble;
output.low_frequency_rumble = scale_output_byte(motor_left);
output.high_frequency_rumble = scale_output_byte(motor_right);
output.raw_report = raw_report;
outputs.push_back(std::move(output));
} else if (valid_flag0 == zero_byte && valid_flag1 == zero_byte) {
GamepadOutput output;
output.kind = GamepadOutputKind::rumble;
output.raw_report = raw_report;
outputs.push_back(std::move(output));
}
if (has_flag(valid_flag0, dualshock4_flag0_lightbar)) {
GamepadOutput output;
output.kind = GamepadOutputKind::rgb_led;
output.red = raw_report[offset + 5U];
output.green = raw_report[offset + 6U];
output.blue = raw_report[offset + 7U];
output.raw_report = raw_report;
outputs.push_back(std::move(output));
}
}
void append_dualsense_outputs(
const ByteReport &report,
const std::vector<std::uint8_t> &raw_report,
std::size_t offset,
std::vector<GamepadOutput> &outputs
) {
const auto valid_flag0 = report[offset];
const auto valid_flag1 = report[offset + 1U];
const auto motor_right = raw_report[offset + 2U];
const auto motor_left = raw_report[offset + 3U];
const auto right_trigger_effect_type = raw_report[offset + 10U];
const auto left_trigger_effect_type = raw_report[offset + 21U];
if (const auto valid_flag2 = report[offset + 38U]; has_flag(valid_flag0, dualsense_flag0_rumble) || has_flag(valid_flag2, dualsense_flag2_compatible_vibration)) {
GamepadOutput output;
output.kind = GamepadOutputKind::rumble;
output.low_frequency_rumble = scale_output_byte(motor_left);
output.high_frequency_rumble = scale_output_byte(motor_right);
output.raw_report = raw_report;
outputs.push_back(std::move(output));
} else if (valid_flag0 == zero_byte && valid_flag1 == zero_byte && valid_flag2 == zero_byte) {
GamepadOutput output;
output.kind = GamepadOutputKind::rumble;
output.raw_report = raw_report;
outputs.push_back(std::move(output));
}
if (has_flag(valid_flag1, dualsense_flag1_lightbar)) {
GamepadOutput output;
output.kind = GamepadOutputKind::rgb_led;
output.red = raw_report[offset + 44U];
output.green = raw_report[offset + 45U];
output.blue = raw_report[offset + 46U];
output.raw_report = raw_report;
outputs.push_back(std::move(output));
}
const auto trigger_flags = valid_flag0 & (dualsense_flag0_left_trigger | dualsense_flag0_right_trigger);
if (trigger_flags != zero_byte) {
GamepadOutput output;
output.kind = GamepadOutputKind::adaptive_triggers;
output.adaptive_trigger_flags = to_uint8(trigger_flags);
output.left_trigger_effect_type = left_trigger_effect_type;
output.right_trigger_effect_type = right_trigger_effect_type;
std::copy_n(raw_report.begin() + static_cast<std::ptrdiff_t>(offset + 11U), output.right_trigger_effect.size(), output.right_trigger_effect.begin());
std::copy_n(raw_report.begin() + static_cast<std::ptrdiff_t>(offset + 22U), output.left_trigger_effect.size(), output.left_trigger_effect.begin());
output.raw_report = raw_report;
outputs.push_back(std::move(output));
}
}
void append_switch_pro_outputs(
const std::vector<std::uint8_t> &report,
std::vector<GamepadOutput> &outputs
) {
if (const auto rumble = decode_switch_rumble_report(report); rumble.has_value()) {
GamepadOutput output;
output.kind = GamepadOutputKind::rumble;
output.low_frequency_rumble = rumble->low_frequency;
output.high_frequency_rumble = rumble->high_frequency;
output.raw_report = report;
outputs.push_back(std::move(output));
}
if (
report.size() >= 12U && report[0] == switch_rumble_and_subcommand_output_report_id &&
report[10] == switch_set_player_lights_subcommand
) {
GamepadOutput output;
output.kind = GamepadOutputKind::player_leds;
const auto player_lights = std::byte {report[11]};
for (std::size_t index = 0; index < output.player_leds.size(); ++index) {
output.player_leds[index] = (player_lights & (std::byte {1} << index)) != zero_byte;
output.flashing_player_leds[index] =