BsMacOSInput.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2017 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include <IOKit/hid/IOHIDLib.h>
  6. namespace bs
  7. {
  8. static constexpr UINT32 HID_NUM_MOUSE_AXES = 3;
  9. static constexpr UINT32 HID_NUM_GAMEPAD_AXES = 24;
  10. /** Available types of devices supported by the HIDManager. */
  11. enum class HIDType
  12. {
  13. Keyboard,
  14. Mouse,
  15. Gamepad
  16. };
  17. /**
  18. * Contains information about a single element of an input device (e.g. a button, an axis), as reported by the
  19. * HIDManager.
  20. */
  21. struct HIDElement
  22. {
  23. IOHIDElementRef ref;
  24. IOHIDElementCookie cookie;
  25. INT32 min, max;
  26. mutable INT32 detectedMin, detectedMax;
  27. UINT32 usage;
  28. };
  29. /** Contains information about a single input device and its elements, as reported by the HIDManager. */
  30. struct HIDDevice
  31. {
  32. IOHIDDeviceRef ref;
  33. IOHIDQueueRef queueRef;
  34. String name;
  35. UINT32 id;
  36. Vector<HIDElement> axes;
  37. Vector<HIDElement> buttons;
  38. Vector<HIDElement> hats;
  39. UINT64 gamepadAxisTimestamps[HID_NUM_GAMEPAD_AXES];
  40. };
  41. /** Contains information about all enumerated input devices for a specific HIDManager. */
  42. struct HIDData
  43. {
  44. Vector<HIDDevice> devices;
  45. HIDType type;
  46. Input* owner = nullptr;
  47. INT32 mouseAxisValues[HID_NUM_MOUSE_AXES];
  48. };
  49. /**
  50. * Provides access to the low level IO HID manager. Enumerates available input devices and reports their input to the
  51. * Input object.
  52. */
  53. class HIDManager
  54. {
  55. public:
  56. /**
  57. * Constructs a new HID manager object.
  58. *
  59. * @param type Determines what category of input devices will this manager enumerate and report events for.
  60. * @param input Input object that will by called by the HID manager when input events occur.
  61. */
  62. HIDManager(HIDType type, Input* input);
  63. ~HIDManager();
  64. /**
  65. * Checks if any new input events have been generates and reports them to the Input object.
  66. *
  67. * @param[in] device Device to read events from. If null, the events are read from all devices of the
  68. * compatible type.
  69. * @param[in] ignoreEvents If true the system will not trigger any external events for the reported input. This
  70. * can be useful for situations where input is disabled, like an out-of-focus window.
  71. */
  72. void capture(IOHIDDeviceRef device, bool ignoreEvents = false);
  73. private:
  74. IOHIDManagerRef mHIDManager = nullptr;
  75. HIDData mData;
  76. };
  77. /** Information about a gamepad. */
  78. struct GamepadInfo
  79. {
  80. UINT32 id;
  81. String name;
  82. IOHIDDeviceRef deviceRef;
  83. HIDManager* hid;
  84. };
  85. /**
  86. * Data specific to MacOS implementation of the input system. Can be passed to platform specific implementations of
  87. * the individual device types.
  88. */
  89. struct InputPrivateData
  90. {
  91. Vector<GamepadInfo> gamepadInfos;
  92. HIDManager* gamepadHIDManager;
  93. };
  94. }