hid_user.cpp 6.7 KB

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