gc_adapter.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. #include <algorithm>
  3. #include <libusb.h>
  4. #include <mutex>
  5. #include <functional>
  6. #include "common/common_types.h"
  7. enum {
  8. PAD_USE_ORIGIN = 0x0080,
  9. PAD_GET_ORIGIN = 0x2000,
  10. PAD_ERR_STATUS = 0x8000,
  11. };
  12. enum PadButton {
  13. PAD_BUTTON_LEFT = 0x0001,
  14. PAD_BUTTON_RIGHT = 0x0002,
  15. PAD_BUTTON_DOWN = 0x0004,
  16. PAD_BUTTON_UP = 0x0008,
  17. PAD_TRIGGER_Z = 0x0010,
  18. PAD_TRIGGER_R = 0x0020,
  19. PAD_TRIGGER_L = 0x0040,
  20. PAD_BUTTON_A = 0x0100,
  21. PAD_BUTTON_B = 0x0200,
  22. PAD_BUTTON_X = 0x0400,
  23. PAD_BUTTON_Y = 0x0800,
  24. PAD_BUTTON_START = 0x1000,
  25. // Below is for compatibility with "AxisButton" type
  26. PAD_STICK = 0x2000,
  27. };
  28. enum PadAxes { STICK_X, STICK_Y, SUBSTICK_X, SUBSTICK_Y, TRIGGER_LEFT, TRIGGER_RIGHT };
  29. struct GCPadStatus {
  30. u16 button; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits
  31. u8 stickX; // 0 <= stickX <= 255
  32. u8 stickY; // 0 <= stickY <= 255
  33. u8 substickX; // 0 <= substickX <= 255
  34. u8 substickY; // 0 <= substickY <= 255
  35. u8 triggerLeft; // 0 <= triggerLeft <= 255
  36. u8 triggerRight; // 0 <= triggerRight <= 255
  37. bool isConnected{true};
  38. static const u8 MAIN_STICK_CENTER_X = 0x80;
  39. static const u8 MAIN_STICK_CENTER_Y = 0x80;
  40. static const u8 MAIN_STICK_RADIUS = 0x7f;
  41. static const u8 C_STICK_CENTER_X = 0x80;
  42. static const u8 C_STICK_CENTER_Y = 0x80;
  43. static const u8 C_STICK_RADIUS = 0x7f;
  44. static const u8 TRIGGER_CENTER = 20;
  45. static const u8 THRESHOLD = 10;
  46. u8 port;
  47. u8 axis_which = 255;
  48. u8 axis_value = 255;
  49. };
  50. struct GCState {
  51. std::unordered_map<int, bool> buttons;
  52. std::unordered_map<int, u16> axes;
  53. };
  54. namespace GCAdapter {
  55. enum ControllerTypes {
  56. CONTROLLER_NONE = 0,
  57. CONTROLLER_WIRED = 1,
  58. CONTROLLER_WIRELESS = 2
  59. };
  60. enum {
  61. NO_ADAPTER_DETECTED = 0,
  62. ADAPTER_DETECTED = 1,
  63. };
  64. // Current adapter status: detected/not detected/in error (holds the error code)
  65. static int current_status = NO_ADAPTER_DETECTED;
  66. GCPadStatus CheckStatus(int port, u8 adapter_payload[37]);
  67. /// Initialize the GC Adapter capture and read sequence
  68. void Init();
  69. /// Close the adapter read thread and release the adapter
  70. void Shutdown();
  71. /// Begin scanning for the GC Adapter.
  72. void StartScanThread();
  73. /// Stop scanning for the adapter
  74. void StopScanThread();
  75. /// Returns true if there is a device connected to port
  76. bool DeviceConnected(int port);
  77. /// Resets status of device connected to port
  78. void ResetDeviceType(int port);
  79. /// Returns true if we successfully gain access to GC Adapter
  80. bool CheckDeviceAccess(libusb_device* device);
  81. /// Captures GC Adapter endpoint address,
  82. void GetGCEndpoint(libusb_device* device);
  83. /// For shutting down, clear all data, join all threads, release usb
  84. void Reset();
  85. /// For use in initialization, querying devices to find the adapter
  86. void Setup();
  87. /// Used for polling
  88. void BeginConfiguration();
  89. void EndConfiguration();
  90. } // end of namespace GCAdapter