diff options
| author | 2014-09-03 18:12:58 -0700 | |
|---|---|---|
| committer | 2014-09-11 22:43:42 -0700 | |
| commit | 4a94ec934ab1a2216f94e3fcc46f5dde1d6e2f02 (patch) | |
| tree | 2588f0c6051c9a5e3f23057d2953c35a854dbc43 /src/core/hle | |
| parent | Created structure for PAD. (diff) | |
| download | yuzu-4a94ec934ab1a2216f94e3fcc46f5dde1d6e2f02.tar.gz yuzu-4a94ec934ab1a2216f94e3fcc46f5dde1d6e2f02.tar.xz yuzu-4a94ec934ab1a2216f94e3fcc46f5dde1d6e2f02.zip | |
Initial HID PAD work, with GLFW only.
Diffstat (limited to 'src/core/hle')
| -rw-r--r-- | src/core/hle/service/hid.cpp | 134 | ||||
| -rw-r--r-- | src/core/hle/service/hid.h | 87 |
2 files changed, 197 insertions, 24 deletions
diff --git a/src/core/hle/service/hid.cpp b/src/core/hle/service/hid.cpp index 6a4895c90..1ef39fced 100644 --- a/src/core/hle/service/hid.cpp +++ b/src/core/hle/service/hid.cpp | |||
| @@ -16,6 +16,121 @@ namespace HID_User { | |||
| 16 | 16 | ||
| 17 | Handle g_shared_mem = 0; ///< Handle to shared memory region designated to HID_User service | 17 | Handle g_shared_mem = 0; ///< Handle to shared memory region designated to HID_User service |
| 18 | 18 | ||
| 19 | // Event handles | ||
| 20 | Handle g_event_pad_or_touch_1 = 0; | ||
| 21 | Handle g_event_pad_or_touch_2 = 0; | ||
| 22 | Handle g_event_accelerometer = 0; | ||
| 23 | Handle g_event_gyroscope = 0; | ||
| 24 | Handle g_event_debug_pad = 0; | ||
| 25 | |||
| 26 | // Next PAD state update information | ||
| 27 | PADState g_next_state = {{0}}; | ||
| 28 | u32 g_next_index = 0; | ||
| 29 | s16 g_next_circle_x = 0; | ||
| 30 | s16 g_next_circle_y = 0; | ||
| 31 | |||
| 32 | /** Gets a pointer to the PADData structure inside HID shared memory | ||
| 33 | */ | ||
| 34 | static inline PADData* GetPADData() { | ||
| 35 | if (0 == g_shared_mem) | ||
| 36 | return nullptr; | ||
| 37 | |||
| 38 | return reinterpret_cast<PADData*>(Kernel::GetSharedMemoryPointer(g_shared_mem, 0)); | ||
| 39 | } | ||
| 40 | |||
| 41 | /** Circle PAD from keys. | ||
| 42 | * | ||
| 43 | * This is implemented as "pushed all the way to an edge (max) or centered (0)". | ||
| 44 | * | ||
| 45 | * Indicate the circle pad is pushed completely to the edge in 1 of 8 directions. | ||
| 46 | */ | ||
| 47 | void UpdateNextCirclePADState() { | ||
| 48 | static const s16 max_value = 0x9C; | ||
| 49 | g_next_circle_x = g_next_state.circle_left ? -max_value : 0x0; | ||
| 50 | g_next_circle_x += g_next_state.circle_right ? max_value : 0x0; | ||
| 51 | g_next_circle_y = g_next_state.circle_down ? -max_value : 0x0; | ||
| 52 | g_next_circle_y += g_next_state.circle_up ? max_value : 0x0; | ||
| 53 | } | ||
| 54 | |||
| 55 | /** Sets a PAD state (button or button combo) as pressed | ||
| 56 | */ | ||
| 57 | void PADButtonPress(PADState pad_state) { | ||
| 58 | g_next_state.hex |= pad_state.hex; | ||
| 59 | UpdateNextCirclePADState(); | ||
| 60 | } | ||
| 61 | |||
| 62 | /** Sets a PAD state (button or button combo) as released | ||
| 63 | */ | ||
| 64 | void PADButtonRelease(PADState pad_state) { | ||
| 65 | g_next_state.hex &= ~pad_state.hex; | ||
| 66 | UpdateNextCirclePADState(); | ||
| 67 | } | ||
| 68 | |||
| 69 | /** Called after all PAD changes to be included in this update have been made, | ||
| 70 | * including both PAD key changes and analog circle PAD changes. | ||
| 71 | */ | ||
| 72 | void PADUpdateComplete() { | ||
| 73 | PADData* pad_data = GetPADData(); | ||
| 74 | |||
| 75 | // Update PADData struct | ||
| 76 | pad_data->current_state.hex = g_next_state.hex; | ||
| 77 | pad_data->index = g_next_index; | ||
| 78 | g_next_index = (g_next_index + 1) % pad_data->entries.size(); | ||
| 79 | |||
| 80 | // Get the previous PAD state | ||
| 81 | u32 last_entry_index = (pad_data->index - 1) % pad_data->entries.size(); | ||
| 82 | PADState old_state = pad_data->entries[last_entry_index].current_state; | ||
| 83 | |||
| 84 | // Compute bitmask with 1s for bits different from the old state | ||
| 85 | PADState changed; | ||
| 86 | changed.hex = (g_next_state.hex ^ old_state.hex); | ||
| 87 | |||
| 88 | // Compute what was added | ||
| 89 | PADState additions; | ||
| 90 | additions.hex = changed.hex & g_next_state.hex; | ||
| 91 | |||
| 92 | // Compute what was removed | ||
| 93 | PADState removals; | ||
| 94 | removals.hex = changed.hex & old_state.hex; | ||
| 95 | |||
| 96 | // Get the current PAD entry | ||
| 97 | PADDataEntry* current_pad_entry = &pad_data->entries[pad_data->index]; | ||
| 98 | |||
| 99 | // Update entry properties | ||
| 100 | current_pad_entry->current_state.hex = g_next_state.hex; | ||
| 101 | current_pad_entry->delta_additions.hex = additions.hex; | ||
| 102 | current_pad_entry->delta_removals.hex = removals.hex; | ||
| 103 | |||
| 104 | // Set circle PAD | ||
| 105 | current_pad_entry->circle_pad_x = g_next_circle_x; | ||
| 106 | current_pad_entry->circle_pad_y = g_next_circle_y; | ||
| 107 | |||
| 108 | // If we just updated index 0, provide a new timestamp | ||
| 109 | if (pad_data->index == 0) { | ||
| 110 | pad_data->index_reset_ticks_previous = pad_data->index_reset_ticks; | ||
| 111 | pad_data->index_reset_ticks = (s64)Core::g_app_core->GetTicks(); | ||
| 112 | } | ||
| 113 | |||
| 114 | // Signal both handles when there's an update to PAD or touch | ||
| 115 | Kernel::SignalEvent(g_event_pad_or_touch_1); | ||
| 116 | Kernel::SignalEvent(g_event_pad_or_touch_2); | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | // TODO(peachum): | ||
| 121 | // Add a method for setting analog input from joystick device for the circle PAD. | ||
| 122 | // | ||
| 123 | // This method should: | ||
| 124 | // * Be called after both PADButton<Press, Release>(). | ||
| 125 | // * Be called before PADUpdateComplete() | ||
| 126 | // * Set current PADEntry.circle_pad_<axis> using analog data | ||
| 127 | // * Set PadData.raw_circle_pad_data | ||
| 128 | // * Set PadData.current_state.circle_right = 1 if current PADEntry.circle_pad_x >= 41 | ||
| 129 | // * Set PadData.current_state.circle_up = 1 if current PADEntry.circle_pad_y >= 41 | ||
| 130 | // * Set PadData.current_state.circle_left = 1 if current PADEntry.circle_pad_x <= -41 | ||
| 131 | // * Set PadData.current_state.circle_right = 1 if current PADEntry.circle_pad_y <= -41 | ||
| 132 | |||
| 133 | |||
| 19 | /** | 134 | /** |
| 20 | * HID_User::GetIPCHandles service function | 135 | * HID_User::GetIPCHandles service function |
| 21 | * Inputs: | 136 | * Inputs: |
| @@ -35,11 +150,11 @@ void GetIPCHandles(Service::Interface* self) { | |||
| 35 | 150 | ||
| 36 | cmd_buff[1] = 0; // No error | 151 | cmd_buff[1] = 0; // No error |
| 37 | cmd_buff[3] = g_shared_mem; | 152 | cmd_buff[3] = g_shared_mem; |
| 38 | cmd_buff[4] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventA"); | 153 | cmd_buff[4] = g_event_pad_or_touch_1; |
| 39 | cmd_buff[5] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventB"); | 154 | cmd_buff[5] = g_event_pad_or_touch_2; |
| 40 | cmd_buff[6] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventC"); | 155 | cmd_buff[6] = g_event_accelerometer; |
| 41 | cmd_buff[7] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventGyroscope"); | 156 | cmd_buff[7] = g_event_gyroscope; |
| 42 | cmd_buff[8] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventD"); | 157 | cmd_buff[8] = g_event_debug_pad; |
| 43 | 158 | ||
| 44 | DEBUG_LOG(KERNEL, "called"); | 159 | DEBUG_LOG(KERNEL, "called"); |
| 45 | } | 160 | } |
| @@ -58,14 +173,19 @@ const Interface::FunctionInfo FunctionTable[] = { | |||
| 58 | }; | 173 | }; |
| 59 | 174 | ||
| 60 | 175 | ||
| 61 | |||
| 62 | |||
| 63 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 176 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 64 | // Interface class | 177 | // Interface class |
| 65 | 178 | ||
| 66 | Interface::Interface() { | 179 | Interface::Interface() { |
| 67 | g_shared_mem = Kernel::CreateSharedMemory("HID_User:SharedMem"); // Create shared memory object | 180 | g_shared_mem = Kernel::CreateSharedMemory("HID_User:SharedMem"); // Create shared memory object |
| 68 | 181 | ||
| 182 | // Create event handles | ||
| 183 | g_event_pad_or_touch_1 = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventPADOrTouch1"); | ||
| 184 | g_event_pad_or_touch_2 = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventPADOrTouch2"); | ||
| 185 | g_event_accelerometer = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventAccelerometer"); | ||
| 186 | g_event_gyroscope = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventGyroscope"); | ||
| 187 | g_event_debug_pad = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventDebugPAD"); | ||
| 188 | |||
| 69 | Register(FunctionTable, ARRAY_SIZE(FunctionTable)); | 189 | Register(FunctionTable, ARRAY_SIZE(FunctionTable)); |
| 70 | } | 190 | } |
| 71 | 191 | ||
diff --git a/src/core/hle/service/hid.h b/src/core/hle/service/hid.h index 6ddf2f80f..f5f76f0fc 100644 --- a/src/core/hle/service/hid.h +++ b/src/core/hle/service/hid.h | |||
| @@ -15,30 +15,83 @@ | |||
| 15 | 15 | ||
| 16 | namespace HID_User { | 16 | namespace HID_User { |
| 17 | 17 | ||
| 18 | /// Structure of a PAD controller state | ||
| 18 | struct PADState { | 19 | struct PADState { |
| 19 | union { | 20 | union { |
| 20 | u32 hex; | 21 | u32 hex; |
| 21 | 22 | ||
| 22 | BitField<0, 1, u32> A; | 23 | BitField<0, 1, u32> a; |
| 23 | BitField<1, 1, u32> B; | 24 | BitField<1, 1, u32> b; |
| 24 | BitField<2, 1, u32> Select; | 25 | BitField<2, 1, u32> select; |
| 25 | BitField<3, 1, u32> Start; | 26 | BitField<3, 1, u32> start; |
| 26 | BitField<4, 1, u32> Right; | 27 | BitField<4, 1, u32> right; |
| 27 | BitField<5, 1, u32> Left; | 28 | BitField<5, 1, u32> left; |
| 28 | BitField<6, 1, u32> Up; | 29 | BitField<6, 1, u32> up; |
| 29 | BitField<7, 1, u32> Down; | 30 | BitField<7, 1, u32> down; |
| 30 | BitField<8, 1, u32> R; | 31 | BitField<8, 1, u32> r; |
| 31 | BitField<9, 1, u32> L; | 32 | BitField<9, 1, u32> l; |
| 32 | BitField<10, 1, u32> X; | 33 | BitField<10, 1, u32> x; |
| 33 | BitField<11, 1, u32> Y; | 34 | BitField<11, 1, u32> y; |
| 34 | 35 | ||
| 35 | BitField<28, 1, u32> CircleRight; | 36 | BitField<28, 1, u32> circle_right; |
| 36 | BitField<29, 1, u32> CircleLeft; | 37 | BitField<29, 1, u32> circle_left; |
| 37 | BitField<30, 1, u32> CircleUp; | 38 | BitField<30, 1, u32> circle_up; |
| 38 | BitField<31, 1, u32> CircleDown; | 39 | BitField<31, 1, u32> circle_down; |
| 39 | }; | 40 | }; |
| 40 | }; | 41 | }; |
| 41 | 42 | ||
| 43 | /// Structure of a single entry in the PADData's PAD state history array | ||
| 44 | struct PADDataEntry { | ||
| 45 | PADState current_state; | ||
| 46 | PADState delta_additions; | ||
| 47 | PADState delta_removals; | ||
| 48 | |||
| 49 | s16 circle_pad_x; | ||
| 50 | s16 circle_pad_y; | ||
| 51 | }; | ||
| 52 | |||
| 53 | /// Structure of all data related to the 3DS Pad | ||
| 54 | struct PADData { | ||
| 55 | s64 index_reset_ticks; | ||
| 56 | s64 index_reset_ticks_previous; | ||
| 57 | u32 index; // the index of the last updated PAD state history element | ||
| 58 | |||
| 59 | u32 pad1; | ||
| 60 | u32 pad2; | ||
| 61 | |||
| 62 | PADState current_state; // same as entries[index].current_state | ||
| 63 | u32 raw_circle_pad_data; | ||
| 64 | |||
| 65 | u32 pad3; | ||
| 66 | |||
| 67 | std::array<PADDataEntry, 8> entries; // PAD state history | ||
| 68 | }; | ||
| 69 | |||
| 70 | // Pre-defined PADStates for single button presses | ||
| 71 | const PADState PAD_NONE = {{0}}; | ||
| 72 | const PADState PAD_A = {{1u << 0}}; | ||
| 73 | const PADState PAD_B = {{1u << 1}}; | ||
| 74 | const PADState PAD_SELECT = {{1u << 2}}; | ||
| 75 | const PADState PAD_START = {{1u << 3}}; | ||
| 76 | const PADState PAD_RIGHT = {{1u << 4}}; | ||
| 77 | const PADState PAD_LEFT = {{1u << 5}}; | ||
| 78 | const PADState PAD_UP = {{1u << 6}}; | ||
| 79 | const PADState PAD_DOWN = {{1u << 7}}; | ||
| 80 | const PADState PAD_R = {{1u << 8}}; | ||
| 81 | const PADState PAD_L = {{1u << 9}}; | ||
| 82 | const PADState PAD_X = {{1u << 10}}; | ||
| 83 | const PADState PAD_Y = {{1u << 11}}; | ||
| 84 | const PADState PAD_CIRCLE_RIGHT = {{1u << 28}}; | ||
| 85 | const PADState PAD_CIRCLE_LEFT = {{1u << 29}}; | ||
| 86 | const PADState PAD_CIRCLE_UP = {{1u << 30}}; | ||
| 87 | const PADState PAD_CIRCLE_DOWN = {{1u << 31}}; | ||
| 88 | |||
| 89 | // Methods for updating the HID module's state | ||
| 90 | void PADButtonPress(PADState pad_state); | ||
| 91 | void PADButtonRelease(PADState pad_state); | ||
| 92 | void PADUpdateComplete(); | ||
| 93 | |||
| 94 | /// HID service interface | ||
| 42 | class Interface : public Service::Interface { | 95 | class Interface : public Service::Interface { |
| 43 | public: | 96 | public: |
| 44 | 97 | ||