joypad_uwp.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************************/
  2. /* joypad_uwp.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "joypad_uwp.h"
  31. #include "core/os/os.h"
  32. using namespace Windows::Gaming::Input;
  33. using namespace Windows::Foundation;
  34. void JoypadUWP::register_events() {
  35. Gamepad::GamepadAdded +=
  36. ref new EventHandler<Gamepad ^>(this, &JoypadUWP::OnGamepadAdded);
  37. Gamepad::GamepadRemoved +=
  38. ref new EventHandler<Gamepad ^>(this, &JoypadUWP::OnGamepadRemoved);
  39. }
  40. void JoypadUWP::process_controllers() {
  41. for (int i = 0; i < MAX_CONTROLLERS; i++) {
  42. ControllerDevice &joy = controllers[i];
  43. if (!joy.connected) break;
  44. switch (joy.type) {
  45. case ControllerType::GAMEPAD_CONTROLLER: {
  46. GamepadReading reading = ((Gamepad ^) joy.controller_reference)->GetCurrentReading();
  47. int button_mask = (int)GamepadButtons::Menu;
  48. for (int j = 0; j < 14; j++) {
  49. input->joy_button(joy.id, j, (int)reading.Buttons & button_mask);
  50. button_mask *= 2;
  51. }
  52. input->joy_axis(joy.id, JOY_AXIS_0, axis_correct(reading.LeftThumbstickX));
  53. input->joy_axis(joy.id, JOY_AXIS_1, axis_correct(reading.LeftThumbstickY, true));
  54. input->joy_axis(joy.id, JOY_AXIS_2, axis_correct(reading.RightThumbstickX));
  55. input->joy_axis(joy.id, JOY_AXIS_3, axis_correct(reading.RightThumbstickY, true));
  56. input->joy_axis(joy.id, JOY_AXIS_4, axis_correct(reading.LeftTrigger, false, true));
  57. input->joy_axis(joy.id, JOY_AXIS_5, axis_correct(reading.RightTrigger, false, true));
  58. uint64_t timestamp = input->get_joy_vibration_timestamp(joy.id);
  59. if (timestamp > joy.ff_timestamp) {
  60. Vector2 strength = input->get_joy_vibration_strength(joy.id);
  61. float duration = input->get_joy_vibration_duration(joy.id);
  62. if (strength.x == 0 && strength.y == 0) {
  63. joypad_vibration_stop(i, timestamp);
  64. } else {
  65. joypad_vibration_start(i, strength.x, strength.y, duration, timestamp);
  66. }
  67. } else if (joy.vibrating && joy.ff_end_timestamp != 0) {
  68. uint64_t current_time = OS::get_singleton()->get_ticks_usec();
  69. if (current_time >= joy.ff_end_timestamp)
  70. joypad_vibration_stop(i, current_time);
  71. }
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. JoypadUWP::JoypadUWP() {
  78. for (int i = 0; i < MAX_CONTROLLERS; i++)
  79. controllers[i].id = i;
  80. }
  81. JoypadUWP::JoypadUWP(InputDefault *p_input) {
  82. input = p_input;
  83. JoypadUWP();
  84. }
  85. void JoypadUWP::OnGamepadAdded(Platform::Object ^ sender, Windows::Gaming::Input::Gamepad ^ value) {
  86. short idx = -1;
  87. for (int i = 0; i < MAX_CONTROLLERS; i++) {
  88. if (!controllers[i].connected) {
  89. idx = i;
  90. break;
  91. }
  92. }
  93. ERR_FAIL_COND(idx == -1);
  94. controllers[idx].connected = true;
  95. controllers[idx].controller_reference = value;
  96. controllers[idx].id = idx;
  97. controllers[idx].type = ControllerType::GAMEPAD_CONTROLLER;
  98. input->joy_connection_changed(controllers[idx].id, true, "Xbox Controller", "__UWP_GAMEPAD__");
  99. }
  100. void JoypadUWP::OnGamepadRemoved(Platform::Object ^ sender, Windows::Gaming::Input::Gamepad ^ value) {
  101. short idx = -1;
  102. for (int i = 0; i < MAX_CONTROLLERS; i++) {
  103. if (controllers[i].controller_reference == value) {
  104. idx = i;
  105. break;
  106. }
  107. }
  108. ERR_FAIL_COND(idx == -1);
  109. controllers[idx] = ControllerDevice();
  110. input->joy_connection_changed(idx, false, "Xbox Controller");
  111. }
  112. InputDefault::JoyAxis JoypadUWP::axis_correct(double p_val, bool p_negate, bool p_trigger) const {
  113. InputDefault::JoyAxis jx;
  114. jx.min = p_trigger ? 0 : -1;
  115. jx.value = (float)(p_negate ? -p_val : p_val);
  116. return jx;
  117. }
  118. void JoypadUWP::joypad_vibration_start(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) {
  119. ControllerDevice &joy = controllers[p_device];
  120. if (joy.connected) {
  121. GamepadVibration vibration;
  122. vibration.LeftMotor = p_strong_magnitude;
  123. vibration.RightMotor = p_weak_magnitude;
  124. ((Gamepad ^) joy.controller_reference)->Vibration = vibration;
  125. joy.ff_timestamp = p_timestamp;
  126. joy.ff_end_timestamp = p_duration == 0 ? 0 : p_timestamp + (uint64_t)(p_duration * 1000000.0);
  127. joy.vibrating = true;
  128. }
  129. }
  130. void JoypadUWP::joypad_vibration_stop(int p_device, uint64_t p_timestamp) {
  131. ControllerDevice &joy = controllers[p_device];
  132. if (joy.connected) {
  133. GamepadVibration vibration;
  134. vibration.LeftMotor = 0.0;
  135. vibration.RightMotor = 0.0;
  136. ((Gamepad ^) joy.controller_reference)->Vibration = vibration;
  137. joy.ff_timestamp = p_timestamp;
  138. joy.vibrating = false;
  139. }
  140. }