hid_user.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/log.h"
  5. #include "core/arm/arm_interface.h"
  6. #include "core/hle/hle.h"
  7. #include "core/hle/kernel/event.h"
  8. #include "core/hle/kernel/shared_memory.h"
  9. #include "hid_user.h"
  10. ////////////////////////////////////////////////////////////////////////////////////////////////////
  11. // Namespace HID_User
  12. namespace HID_User {
  13. // Handle to shared memory region designated to HID_User service
  14. static Handle shared_mem = 0;
  15. // Event handles
  16. static Handle event_pad_or_touch_1 = 0;
  17. static Handle event_pad_or_touch_2 = 0;
  18. static Handle event_accelerometer = 0;
  19. static Handle event_gyroscope = 0;
  20. static Handle event_debug_pad = 0;
  21. // Next Pad state update information
  22. static PadState next_state = {{0}};
  23. static u32 next_index = 0;
  24. static s16 next_circle_x = 0;
  25. static s16 next_circle_y = 0;
  26. /**
  27. * Gets a pointer to the PadData structure inside HID shared memory
  28. */
  29. static inline PadData* GetPadData() {
  30. return reinterpret_cast<PadData*>(Kernel::GetSharedMemoryPointer(shared_mem, 0).ValueOr(nullptr));
  31. }
  32. /**
  33. * Circle Pad from keys.
  34. *
  35. * This is implemented as "pushed all the way to an edge (max) or centered (0)".
  36. *
  37. * Indicate the circle pad is pushed completely to the edge in 1 of 8 directions.
  38. */
  39. static void UpdateNextCirclePadState() {
  40. static const s16 max_value = 0x9C;
  41. next_circle_x = next_state.circle_left ? -max_value : 0x0;
  42. next_circle_x += next_state.circle_right ? max_value : 0x0;
  43. next_circle_y = next_state.circle_down ? -max_value : 0x0;
  44. next_circle_y += next_state.circle_up ? max_value : 0x0;
  45. }
  46. /**
  47. * Sets a Pad state (button or button combo) as pressed
  48. */
  49. void PadButtonPress(const PadState& pad_state) {
  50. next_state.hex |= pad_state.hex;
  51. UpdateNextCirclePadState();
  52. }
  53. /**
  54. * Sets a Pad state (button or button combo) as released
  55. */
  56. void PadButtonRelease(const PadState& pad_state) {
  57. next_state.hex &= ~pad_state.hex;
  58. UpdateNextCirclePadState();
  59. }
  60. /**
  61. * Called after all Pad changes to be included in this update have been made,
  62. * including both Pad key changes and analog circle Pad changes.
  63. */
  64. void PadUpdateComplete() {
  65. PadData* pad_data = GetPadData();
  66. if (pad_data == nullptr) {
  67. return;
  68. }
  69. // Update PadData struct
  70. pad_data->current_state.hex = next_state.hex;
  71. pad_data->index = next_index;
  72. next_index = (next_index + 1) % pad_data->entries.size();
  73. // Get the previous Pad state
  74. u32 last_entry_index = (pad_data->index - 1) % pad_data->entries.size();
  75. PadState old_state = pad_data->entries[last_entry_index].current_state;
  76. // Compute bitmask with 1s for bits different from the old state
  77. PadState changed;
  78. changed.hex = (next_state.hex ^ old_state.hex);
  79. // Compute what was added
  80. PadState additions;
  81. additions.hex = changed.hex & next_state.hex;
  82. // Compute what was removed
  83. PadState removals;
  84. removals.hex = changed.hex & old_state.hex;
  85. // Get the current Pad entry
  86. PadDataEntry* current_pad_entry = &pad_data->entries[pad_data->index];
  87. // Update entry properties
  88. current_pad_entry->current_state.hex = next_state.hex;
  89. current_pad_entry->delta_additions.hex = additions.hex;
  90. current_pad_entry->delta_removals.hex = removals.hex;
  91. // Set circle Pad
  92. current_pad_entry->circle_pad_x = next_circle_x;
  93. current_pad_entry->circle_pad_y = next_circle_y;
  94. // If we just updated index 0, provide a new timestamp
  95. if (pad_data->index == 0) {
  96. pad_data->index_reset_ticks_previous = pad_data->index_reset_ticks;
  97. pad_data->index_reset_ticks = (s64)Core::g_app_core->GetTicks();
  98. }
  99. // Signal both handles when there's an update to Pad or touch
  100. Kernel::SignalEvent(event_pad_or_touch_1);
  101. Kernel::SignalEvent(event_pad_or_touch_2);
  102. }
  103. // TODO(peachum):
  104. // Add a method for setting analog input from joystick device for the circle Pad.
  105. //
  106. // This method should:
  107. // * Be called after both PadButton<Press, Release>().
  108. // * Be called before PadUpdateComplete()
  109. // * Set current PadEntry.circle_pad_<axis> using analog data
  110. // * Set PadData.raw_circle_pad_data
  111. // * Set PadData.current_state.circle_right = 1 if current PadEntry.circle_pad_x >= 41
  112. // * Set PadData.current_state.circle_up = 1 if current PadEntry.circle_pad_y >= 41
  113. // * Set PadData.current_state.circle_left = 1 if current PadEntry.circle_pad_x <= -41
  114. // * Set PadData.current_state.circle_right = 1 if current PadEntry.circle_pad_y <= -41
  115. /**
  116. * HID_User::GetIPCHandles service function
  117. * Inputs:
  118. * None
  119. * Outputs:
  120. * 1 : Result of function, 0 on success, otherwise error code
  121. * 2 : Unused
  122. * 3 : Handle to HID_User shared memory
  123. * 4 : Event signaled by HID_User
  124. * 5 : Event signaled by HID_User
  125. * 6 : Event signaled by HID_User
  126. * 7 : Gyroscope event
  127. * 8 : Event signaled by HID_User
  128. */
  129. static void GetIPCHandles(Service::Interface* self) {
  130. u32* cmd_buff = Kernel::GetCommandBuffer();
  131. cmd_buff[1] = 0; // No error
  132. cmd_buff[3] = shared_mem;
  133. cmd_buff[4] = event_pad_or_touch_1;
  134. cmd_buff[5] = event_pad_or_touch_2;
  135. cmd_buff[6] = event_accelerometer;
  136. cmd_buff[7] = event_gyroscope;
  137. cmd_buff[8] = event_debug_pad;
  138. LOG_TRACE(Service_HID, "called");
  139. }
  140. const Interface::FunctionInfo FunctionTable[] = {
  141. {0x000A0000, GetIPCHandles, "GetIPCHandles"},
  142. {0x000B0000, nullptr, "StartAnalogStickCalibration"},
  143. {0x000E0000, nullptr, "GetAnalogStickCalibrateParam"},
  144. {0x00110000, nullptr, "EnableAccelerometer"},
  145. {0x00120000, nullptr, "DisableAccelerometer"},
  146. {0x00130000, nullptr, "EnableGyroscopeLow"},
  147. {0x00140000, nullptr, "DisableGyroscopeLow"},
  148. {0x00150000, nullptr, "GetGyroscopeLowRawToDpsCoefficient"},
  149. {0x00160000, nullptr, "GetGyroscopeLowCalibrateParam"},
  150. {0x00170000, nullptr, "GetSoundVolume"},
  151. };
  152. ////////////////////////////////////////////////////////////////////////////////////////////////////
  153. // Interface class
  154. Interface::Interface() {
  155. shared_mem = Kernel::CreateSharedMemory("HID_User:SharedMem"); // Create shared memory object
  156. // Create event handles
  157. event_pad_or_touch_1 = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventPadOrTouch1");
  158. event_pad_or_touch_2 = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventPadOrTouch2");
  159. event_accelerometer = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventAccelerometer");
  160. event_gyroscope = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventGyroscope");
  161. event_debug_pad = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventDebugPad");
  162. Register(FunctionTable, ARRAY_SIZE(FunctionTable));
  163. }
  164. } // namespace