hid.cpp 6.6 KB

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