summaryrefslogtreecommitdiff
path: root/src/core
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
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')
-rw-r--r--src/core/hle/service/hid.cpp145
-rw-r--r--src/core/hle/service/hid.h90
2 files changed, 225 insertions, 10 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}
diff --git a/src/core/hle/service/hid.h b/src/core/hle/service/hid.h
index b17fcfa86..a077e25cd 100644
--- a/src/core/hle/service/hid.h
+++ b/src/core/hle/service/hid.h
@@ -5,15 +5,101 @@
5#pragma once 5#pragma once
6 6
7#include "core/hle/service/service.h" 7#include "core/hle/service/service.h"
8#include "common/bit_field.h"
8 9
9//////////////////////////////////////////////////////////////////////////////////////////////////// 10////////////////////////////////////////////////////////////////////////////////////////////////////
10// Namespace HID_User 11// Namespace HID_User
11 12
12// This service is used for interfacing to physical user controls... perhaps "Human Interface 13// This service is used for interfacing to physical user controls.
13// Devices"? Uses include game pad controls, accelerometers, gyroscopes, etc. 14// Uses include game pad controls, touchscreen, accelerometers, gyroscopes, and debug pad.
14 15
15namespace HID_User { 16namespace HID_User {
16 17
18/**
19 * Structure of a Pad controller state.
20 */
21struct PadState {
22 union {
23 u32 hex;
24
25 BitField<0, 1, u32> a;
26 BitField<1, 1, u32> b;
27 BitField<2, 1, u32> select;
28 BitField<3, 1, u32> start;
29 BitField<4, 1, u32> right;
30 BitField<5, 1, u32> left;
31 BitField<6, 1, u32> up;
32 BitField<7, 1, u32> down;
33 BitField<8, 1, u32> r;
34 BitField<9, 1, u32> l;
35 BitField<10, 1, u32> x;
36 BitField<11, 1, u32> y;
37
38 BitField<28, 1, u32> circle_right;
39 BitField<29, 1, u32> circle_left;
40 BitField<30, 1, u32> circle_up;
41 BitField<31, 1, u32> circle_down;
42 };
43};
44
45/**
46 * Structure of a single entry in the PadData's Pad state history array.
47 */
48struct PadDataEntry {
49 PadState current_state;
50 PadState delta_additions;
51 PadState delta_removals;
52
53 s16 circle_pad_x;
54 s16 circle_pad_y;
55};
56
57/**
58 * Structure of all data related to the 3DS Pad.
59 */
60struct PadData {
61 s64 index_reset_ticks;
62 s64 index_reset_ticks_previous;
63 u32 index; // the index of the last updated Pad state history element
64
65 u32 pad1;
66 u32 pad2;
67
68 PadState current_state; // same as entries[index].current_state
69 u32 raw_circle_pad_data;
70
71 u32 pad3;
72
73 std::array<PadDataEntry, 8> entries; // Pad state history
74};
75
76// Pre-defined PadStates for single button presses
77const PadState PAD_NONE = {{0}};
78const PadState PAD_A = {{1u << 0}};
79const PadState PAD_B = {{1u << 1}};
80const PadState PAD_SELECT = {{1u << 2}};
81const PadState PAD_START = {{1u << 3}};
82const PadState PAD_RIGHT = {{1u << 4}};
83const PadState PAD_LEFT = {{1u << 5}};
84const PadState PAD_UP = {{1u << 6}};
85const PadState PAD_DOWN = {{1u << 7}};
86const PadState PAD_R = {{1u << 8}};
87const PadState PAD_L = {{1u << 9}};
88const PadState PAD_X = {{1u << 10}};
89const PadState PAD_Y = {{1u << 11}};
90const PadState PAD_CIRCLE_RIGHT = {{1u << 28}};
91const PadState PAD_CIRCLE_LEFT = {{1u << 29}};
92const PadState PAD_CIRCLE_UP = {{1u << 30}};
93const PadState PAD_CIRCLE_DOWN = {{1u << 31}};
94
95// Methods for updating the HID module's state
96void PadButtonPress(PadState pad_state);
97void PadButtonRelease(PadState pad_state);
98void PadUpdateComplete();
99
100/**
101 * HID service interface.
102 */
17class Interface : public Service::Interface { 103class Interface : public Service::Interface {
18public: 104public:
19 105