input.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #if defined(INPUT_CARBON)
  2. #include <ruby/input/carbon.cpp>
  3. #endif
  4. #if defined(INPUT_QUARTZ)
  5. #include <ruby/input/quartz.cpp>
  6. #endif
  7. #if defined(INPUT_SDL)
  8. #include <ruby/input/sdl.cpp>
  9. #endif
  10. #if defined(INPUT_UDEV)
  11. #include <ruby/input/udev.cpp>
  12. #endif
  13. #if defined(INPUT_WINDOWS)
  14. #include <ruby/input/windows.cpp>
  15. #endif
  16. #if defined(INPUT_XLIB)
  17. #include <ruby/input/xlib.cpp>
  18. #endif
  19. namespace ruby {
  20. auto Input::setContext(uintptr context) -> bool {
  21. if(instance->context == context) return true;
  22. if(!instance->hasContext()) return false;
  23. if(!instance->setContext(instance->context = context)) return false;
  24. return true;
  25. }
  26. //
  27. auto Input::acquired() -> bool {
  28. return instance->acquired();
  29. }
  30. auto Input::acquire() -> bool {
  31. return instance->acquire();
  32. }
  33. auto Input::release() -> bool {
  34. return instance->release();
  35. }
  36. auto Input::poll() -> vector<shared_pointer<nall::HID::Device>> {
  37. return instance->poll();
  38. }
  39. auto Input::rumble(uint64_t id, bool enable) -> bool {
  40. return instance->rumble(id, enable);
  41. }
  42. //
  43. auto Input::onChange(const function<void (shared_pointer<HID::Device>, uint, uint, int16_t, int16_t)>& onChange) -> void {
  44. change = onChange;
  45. }
  46. auto Input::doChange(shared_pointer<HID::Device> device, uint group, uint input, int16_t oldValue, int16_t newValue) -> void {
  47. if(change) change(device, group, input, oldValue, newValue);
  48. }
  49. //
  50. auto Input::create(string driver) -> bool {
  51. self.instance.reset();
  52. if(!driver) driver = optimalDriver();
  53. #if defined(INPUT_WINDOWS)
  54. if(driver == "Windows") self.instance = new InputWindows(*this);
  55. #endif
  56. #if defined(INPUT_QUARTZ)
  57. if(driver == "Quartz") self.instance = new InputQuartz(*this);
  58. #endif
  59. #if defined(INPUT_CARBON)
  60. if(driver == "Carbon") self.instance = new InputCarbon(*this);
  61. #endif
  62. #if defined(INPUT_UDEV)
  63. if(driver == "udev") self.instance = new InputUdev(*this);
  64. #endif
  65. #if defined(INPUT_SDL)
  66. if(driver == "SDL") self.instance = new InputSDL(*this);
  67. #endif
  68. #if defined(INPUT_XLIB)
  69. if(driver == "Xlib") self.instance = new InputXlib(*this);
  70. #endif
  71. if(!self.instance) self.instance = new InputDriver(*this);
  72. return self.instance->create();
  73. }
  74. auto Input::hasDrivers() -> vector<string> {
  75. return {
  76. #if defined(INPUT_WINDOWS)
  77. "Windows",
  78. #endif
  79. #if defined(INPUT_QUARTZ)
  80. "Quartz",
  81. #endif
  82. #if defined(INPUT_CARBON)
  83. "Carbon",
  84. #endif
  85. #if defined(INPUT_UDEV)
  86. "udev",
  87. #endif
  88. #if defined(INPUT_SDL)
  89. "SDL",
  90. #endif
  91. #if defined(INPUT_XLIB)
  92. "Xlib",
  93. #endif
  94. "None"};
  95. }
  96. auto Input::optimalDriver() -> string {
  97. #if defined(INPUT_WINDOWS)
  98. return "Windows";
  99. #elif defined(INPUT_QUARTZ)
  100. return "Quartz";
  101. #elif defined(INPUT_CARBON)
  102. return "Carbon";
  103. #elif defined(INPUT_UDEV)
  104. return "udev";
  105. #elif defined(INPUT_SDL)
  106. return "SDL";
  107. #elif defined(INPUT_XLIB)
  108. return "Xlib";
  109. #else
  110. return "None";
  111. #endif
  112. }
  113. auto Input::safestDriver() -> string {
  114. #if defined(INPUT_WINDOWS)
  115. return "Windows";
  116. #elif defined(INPUT_QUARTZ)
  117. return "Quartz";
  118. #elif defined(INPUT_CARBON)
  119. return "Carbon";
  120. #elif defined(INPUT_UDEV)
  121. return "udev";
  122. #elif defined(INPUT_SDL)
  123. return "SDL";
  124. #elif defined(INPUT_XLIB)
  125. return "Xlib";
  126. #else
  127. return "none";
  128. #endif
  129. }
  130. }