hid.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #include <nall/maybe.hpp>
  3. #include <nall/range.hpp>
  4. #include <nall/string.hpp>
  5. #include <nall/vector.hpp>
  6. namespace nall::HID {
  7. struct Input {
  8. Input(const string& name) : _name(name) {}
  9. auto name() const -> string { return _name; }
  10. auto value() const -> int16_t { return _value; }
  11. auto setValue(int16_t value) -> void { _value = value; }
  12. private:
  13. string _name;
  14. int16_t _value = 0;
  15. friend class Group;
  16. };
  17. struct Group : vector<Input> {
  18. Group(const string& name) : _name(name) {}
  19. auto name() const -> string { return _name; }
  20. auto input(uint id) -> Input& { return operator[](id); }
  21. auto append(const string& name) -> void { vector::append(Input{name}); }
  22. auto find(const string& name) const -> maybe<uint> {
  23. for(auto id : range(size())) {
  24. if(operator[](id)._name == name) return id;
  25. }
  26. return nothing;
  27. }
  28. private:
  29. string _name;
  30. friend class Device;
  31. };
  32. struct Device : vector<Group> {
  33. Device(const string& name) : _name(name) {}
  34. //id => {pathID}-{vendorID}-{productID}
  35. auto pathID() const -> uint32_t { return (uint32_t)(_id >> 32); } //32-63
  36. auto vendorID() const -> uint16_t { return (uint16_t)(_id >> 16); } //16-31
  37. auto productID() const -> uint16_t { return (uint16_t)(_id >> 0); } // 0-15
  38. auto setPathID (uint32_t pathID ) -> void { _id = (uint64_t)pathID << 32 | vendorID() << 16 | productID() << 0; }
  39. auto setVendorID (uint16_t vendorID ) -> void { _id = (uint64_t)pathID() << 32 | vendorID << 16 | productID() << 0; }
  40. auto setProductID(uint16_t productID) -> void { _id = (uint64_t)pathID() << 32 | vendorID() << 16 | productID << 0; }
  41. virtual auto isNull() const -> bool { return false; }
  42. virtual auto isKeyboard() const -> bool { return false; }
  43. virtual auto isMouse() const -> bool { return false; }
  44. virtual auto isJoypad() const -> bool { return false; }
  45. auto name() const -> string { return _name; }
  46. auto id() const -> uint64_t { return _id; }
  47. auto setID(uint64_t id) -> void { _id = id; }
  48. auto group(uint id) -> Group& { return operator[](id); }
  49. auto append(const string& name) -> void { vector::append(Group{name}); }
  50. auto find(const string& name) const -> maybe<uint> {
  51. for(auto id : range(size())) {
  52. if(operator[](id)._name == name) return id;
  53. }
  54. return nothing;
  55. }
  56. private:
  57. string _name;
  58. uint64_t _id = 0;
  59. };
  60. struct Null : Device {
  61. enum : uint16_t { GenericVendorID = 0x0000, GenericProductID = 0x0000 };
  62. Null() : Device("Null") {}
  63. auto isNull() const -> bool { return true; }
  64. };
  65. struct Keyboard : Device {
  66. enum : uint16_t { GenericVendorID = 0x0000, GenericProductID = 0x0001 };
  67. enum GroupID : uint { Button };
  68. Keyboard() : Device("Keyboard") { append("Button"); }
  69. auto isKeyboard() const -> bool { return true; }
  70. auto buttons() -> Group& { return group(GroupID::Button); }
  71. };
  72. struct Mouse : Device {
  73. enum : uint16_t { GenericVendorID = 0x0000, GenericProductID = 0x0002 };
  74. enum GroupID : uint { Axis, Button };
  75. Mouse() : Device("Mouse") { append("Axis"), append("Button"); }
  76. auto isMouse() const -> bool { return true; }
  77. auto axes() -> Group& { return group(GroupID::Axis); }
  78. auto buttons() -> Group& { return group(GroupID::Button); }
  79. };
  80. struct Joypad : Device {
  81. enum : uint16_t { GenericVendorID = 0x0000, GenericProductID = 0x0003 };
  82. enum GroupID : uint { Axis, Hat, Trigger, Button };
  83. Joypad() : Device("Joypad") { append("Axis"), append("Hat"), append("Trigger"), append("Button"); }
  84. auto isJoypad() const -> bool { return true; }
  85. auto axes() -> Group& { return group(GroupID::Axis); }
  86. auto hats() -> Group& { return group(GroupID::Hat); }
  87. auto triggers() -> Group& { return group(GroupID::Trigger); }
  88. auto buttons() -> Group& { return group(GroupID::Button); }
  89. auto rumble() const -> bool { return _rumble; }
  90. auto setRumble(bool rumble) -> void { _rumble = rumble; }
  91. private:
  92. bool _rumble = false;
  93. };
  94. }