summaryrefslogtreecommitdiff
path: root/src/core/hle/service/hid.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2014-09-12 17:44:25 -0400
committerGravatar bunnei2014-09-12 17:44:25 -0400
commitcbdf4d4c8e11f8d1503b440eee1ed2d5f144a73f (patch)
treec9c95671835d73b5ca7e52029de5bb27832e11a3 /src/core/hle/service/hid.cpp
parentMerge pull request #99 from archshift/ext-check (diff)
parentAdded support for multiple input device types for KeyMap and connected Qt. (diff)
downloadyuzu-cbdf4d4c8e11f8d1503b440eee1ed2d5f144a73f.tar.gz
yuzu-cbdf4d4c8e11f8d1503b440eee1ed2d5f144a73f.tar.xz
yuzu-cbdf4d4c8e11f8d1503b440eee1ed2d5f144a73f.zip
Merge pull request #105 from kevinhartman/hid
Digital user input and HID module implementation for PAD
Diffstat (limited to 'src/core/hle/service/hid.cpp')
-rw-r--r--src/core/hle/service/hid.cpp145
1 files changed, 137 insertions, 8 deletions
diff --git a/src/core/hle/service/hid.cpp b/src/core/hle/service/hid.cpp
index 4e470795f..b6ec1b8ff 100644
--- a/src/core/hle/service/hid.cpp
+++ b/src/core/hle/service/hid.cpp
@@ -14,7 +14,128 @@
14 14
15namespace HID_User { 15namespace HID_User {
16 16
17Handle g_shared_mem = 0; ///< Handle to shared memory region designated to HID_User service 17// Handle to shared memory region designated to HID_User service
18static Handle shared_mem = 0;
19
20// Event handles
21static Handle event_pad_or_touch_1 = 0;
22static Handle event_pad_or_touch_2 = 0;
23static Handle event_accelerometer = 0;
24static Handle event_gyroscope = 0;
25static Handle event_debug_pad = 0;
26
27// Next Pad state update information
28static PadState next_state = {{0}};
29static u32 next_index = 0;
30static s16 next_circle_x = 0;
31static s16 next_circle_y = 0;
32
33/**
34 * Gets a pointer to the PadData structure inside HID shared memory
35 */
36static inline PadData* GetPadData() {
37 if (0 == shared_mem)
38 return nullptr;
39
40 return reinterpret_cast<PadData*>(Kernel::GetSharedMemoryPointer(shared_mem, 0));
41}
42
43/**
44 * Circle Pad from keys.
45 *
46 * This is implemented as "pushed all the way to an edge (max) or centered (0)".
47 *
48 * Indicate the circle pad is pushed completely to the edge in 1 of 8 directions.
49 */
50void UpdateNextCirclePadState() {
51 static const s16 max_value = 0x9C;
52 next_circle_x = next_state.circle_left ? -max_value : 0x0;
53 next_circle_x += next_state.circle_right ? max_value : 0x0;
54 next_circle_y = next_state.circle_down ? -max_value : 0x0;
55 next_circle_y += next_state.circle_up ? max_value : 0x0;
56}
57
58/**
59 * Sets a Pad state (button or button combo) as pressed
60 */
61void PadButtonPress(PadState pad_state) {
62 next_state.hex |= pad_state.hex;
63 UpdateNextCirclePadState();
64}
65
66/**
67 * Sets a Pad state (button or button combo) as released
68 */
69void PadButtonRelease(PadState pad_state) {
70 next_state.hex &= ~pad_state.hex;
71 UpdateNextCirclePadState();
72}
73
74/**
75 * Called after all Pad changes to be included in this update have been made,
76 * including both Pad key changes and analog circle Pad changes.
77 */
78void PadUpdateComplete() {
79 PadData* pad_data = GetPadData();
80
81 // Update PadData struct
82 pad_data->current_state.hex = next_state.hex;
83 pad_data->index = next_index;
84 next_index = (next_index + 1) % pad_data->entries.size();
85
86 // Get the previous Pad state
87 u32 last_entry_index = (pad_data->index - 1) % pad_data->entries.size();
88 PadState old_state = pad_data->entries[last_entry_index].current_state;
89
90 // Compute bitmask with 1s for bits different from the old state
91 PadState changed;
92 changed.hex = (next_state.hex ^ old_state.hex);
93
94 // Compute what was added
95 PadState additions;
96 additions.hex = changed.hex & next_state.hex;
97
98 // Compute what was removed
99 PadState removals;
100 removals.hex = changed.hex & old_state.hex;
101
102 // Get the current Pad entry
103 PadDataEntry* current_pad_entry = &pad_data->entries[pad_data->index];
104
105 // Update entry properties
106 current_pad_entry->current_state.hex = next_state.hex;
107 current_pad_entry->delta_additions.hex = additions.hex;
108 current_pad_entry->delta_removals.hex = removals.hex;
109
110 // Set circle Pad
111 current_pad_entry->circle_pad_x = next_circle_x;
112 current_pad_entry->circle_pad_y = next_circle_y;
113
114 // If we just updated index 0, provide a new timestamp
115 if (pad_data->index == 0) {
116 pad_data->index_reset_ticks_previous = pad_data->index_reset_ticks;
117 pad_data->index_reset_ticks = (s64)Core::g_app_core->GetTicks();
118 }
119
120 // Signal both handles when there's an update to Pad or touch
121 Kernel::SignalEvent(event_pad_or_touch_1);
122 Kernel::SignalEvent(event_pad_or_touch_2);
123}
124
125
126// TODO(peachum):
127// Add a method for setting analog input from joystick device for the circle Pad.
128//
129// This method should:
130// * Be called after both PadButton<Press, Release>().
131// * Be called before PadUpdateComplete()
132// * Set current PadEntry.circle_pad_<axis> using analog data
133// * Set PadData.raw_circle_pad_data
134// * Set PadData.current_state.circle_right = 1 if current PadEntry.circle_pad_x >= 41
135// * Set PadData.current_state.circle_up = 1 if current PadEntry.circle_pad_y >= 41
136// * Set PadData.current_state.circle_left = 1 if current PadEntry.circle_pad_x <= -41
137// * Set PadData.current_state.circle_right = 1 if current PadEntry.circle_pad_y <= -41
138
18 139
19/** 140/**
20 * HID_User::GetIPCHandles service function 141 * HID_User::GetIPCHandles service function
@@ -34,12 +155,12 @@ void GetIPCHandles(Service::Interface* self) {
34 u32* cmd_buff = Service::GetCommandBuffer(); 155 u32* cmd_buff = Service::GetCommandBuffer();
35 156
36 cmd_buff[1] = 0; // No error 157 cmd_buff[1] = 0; // No error
37 cmd_buff[3] = g_shared_mem; 158 cmd_buff[3] = shared_mem;
38 cmd_buff[4] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventA"); 159 cmd_buff[4] = event_pad_or_touch_1;
39 cmd_buff[5] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventB"); 160 cmd_buff[5] = event_pad_or_touch_2;
40 cmd_buff[6] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventC"); 161 cmd_buff[6] = event_accelerometer;
41 cmd_buff[7] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventGyroscope"); 162 cmd_buff[7] = event_gyroscope;
42 cmd_buff[8] = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventD"); 163 cmd_buff[8] = event_debug_pad;
43 164
44 DEBUG_LOG(KERNEL, "called"); 165 DEBUG_LOG(KERNEL, "called");
45} 166}
@@ -57,11 +178,19 @@ const Interface::FunctionInfo FunctionTable[] = {
57 {0x00170000, nullptr, "GetSoundVolume"}, 178 {0x00170000, nullptr, "GetSoundVolume"},
58}; 179};
59 180
181
60//////////////////////////////////////////////////////////////////////////////////////////////////// 182////////////////////////////////////////////////////////////////////////////////////////////////////
61// Interface class 183// Interface class
62 184
63Interface::Interface() { 185Interface::Interface() {
64 g_shared_mem = Kernel::CreateSharedMemory("HID_User:SharedMem"); // Create shared memory object 186 shared_mem = Kernel::CreateSharedMemory("HID_User:SharedMem"); // Create shared memory object
187
188 // Create event handles
189 event_pad_or_touch_1 = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventPadOrTouch1");
190 event_pad_or_touch_2 = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventPadOrTouch2");
191 event_accelerometer = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventAccelerometer");
192 event_gyroscope = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventGyroscope");
193 event_debug_pad = Kernel::CreateEvent(RESETTYPE_ONESHOT, "HID_User:EventDebugPad");
65 194
66 Register(FunctionTable, ARRAY_SIZE(FunctionTable)); 195 Register(FunctionTable, ARRAY_SIZE(FunctionTable));
67} 196}