summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar bunnei2017-05-10 09:56:27 -0400
committerGravatar GitHub2017-05-10 09:56:27 -0400
commit15b26249cc1b7a66d22bde5835b8a08f3465d898 (patch)
treef147180d0d9a0f8226f95740bc3667ac8c67589d /src
parentMerge pull request #2696 from Subv/vfp_revert (diff)
parentfixup!ir: implement new 3ds HID via ir:rst (diff)
downloadyuzu-15b26249cc1b7a66d22bde5835b8a08f3465d898.tar.gz
yuzu-15b26249cc1b7a66d22bde5835b8a08f3465d898.tar.xz
yuzu-15b26249cc1b7a66d22bde5835b8a08f3465d898.zip
Merge pull request #2676 from wwylele/irrst
ir: implement new 3ds HID via ir:rst
Diffstat (limited to 'src')
-rw-r--r--src/core/hle/service/hid/hid.cpp19
-rw-r--r--src/core/hle/service/hid/hid.h10
-rw-r--r--src/core/hle/service/ir/ir.cpp5
-rw-r--r--src/core/hle/service/ir/ir.h3
-rw-r--r--src/core/hle/service/ir/ir_rst.cpp186
-rw-r--r--src/core/hle/service/ir/ir_rst.h3
-rw-r--r--src/core/hle/service/ir/ir_user.cpp2
-rw-r--r--src/core/hle/service/ir/ir_user.h2
-rw-r--r--src/core/settings.cpp2
9 files changed, 208 insertions, 24 deletions
diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp
index b19e831fe..64d01cdd7 100644
--- a/src/core/hle/service/hid/hid.cpp
+++ b/src/core/hle/service/hid/hid.cpp
@@ -53,30 +53,29 @@ static std::array<std::unique_ptr<Input::ButtonDevice>, Settings::NativeButton::
53 buttons; 53 buttons;
54static std::unique_ptr<Input::AnalogDevice> circle_pad; 54static std::unique_ptr<Input::AnalogDevice> circle_pad;
55 55
56static PadState GetCirclePadDirectionState(s16 circle_pad_x, s16 circle_pad_y) { 56DirectionState GetStickDirectionState(s16 circle_pad_x, s16 circle_pad_y) {
57 // 30 degree and 60 degree are angular thresholds for directions 57 // 30 degree and 60 degree are angular thresholds for directions
58 constexpr float TAN30 = 0.577350269f; 58 constexpr float TAN30 = 0.577350269f;
59 constexpr float TAN60 = 1 / TAN30; 59 constexpr float TAN60 = 1 / TAN30;
60 // a circle pad radius greater than 40 will trigger circle pad direction 60 // a circle pad radius greater than 40 will trigger circle pad direction
61 constexpr int CIRCLE_PAD_THRESHOLD_SQUARE = 40 * 40; 61 constexpr int CIRCLE_PAD_THRESHOLD_SQUARE = 40 * 40;
62 PadState state; 62 DirectionState state{false, false, false, false};
63 state.hex = 0;
64 63
65 if (circle_pad_x * circle_pad_x + circle_pad_y * circle_pad_y > CIRCLE_PAD_THRESHOLD_SQUARE) { 64 if (circle_pad_x * circle_pad_x + circle_pad_y * circle_pad_y > CIRCLE_PAD_THRESHOLD_SQUARE) {
66 float t = std::abs(static_cast<float>(circle_pad_y) / circle_pad_x); 65 float t = std::abs(static_cast<float>(circle_pad_y) / circle_pad_x);
67 66
68 if (circle_pad_x != 0 && t < TAN60) { 67 if (circle_pad_x != 0 && t < TAN60) {
69 if (circle_pad_x > 0) 68 if (circle_pad_x > 0)
70 state.circle_right.Assign(1); 69 state.right = true;
71 else 70 else
72 state.circle_left.Assign(1); 71 state.left = true;
73 } 72 }
74 73
75 if (circle_pad_x == 0 || t > TAN30) { 74 if (circle_pad_x == 0 || t > TAN30) {
76 if (circle_pad_y > 0) 75 if (circle_pad_y > 0)
77 state.circle_up.Assign(1); 76 state.up = true;
78 else 77 else
79 state.circle_down.Assign(1); 78 state.down = true;
80 } 79 }
81 } 80 }
82 81
@@ -125,7 +124,11 @@ static void UpdatePadCallback(u64 userdata, int cycles_late) {
125 constexpr int MAX_CIRCLEPAD_POS = 0x9C; // Max value for a circle pad position 124 constexpr int MAX_CIRCLEPAD_POS = 0x9C; // Max value for a circle pad position
126 s16 circle_pad_x = static_cast<s16>(circle_pad_x_f * MAX_CIRCLEPAD_POS); 125 s16 circle_pad_x = static_cast<s16>(circle_pad_x_f * MAX_CIRCLEPAD_POS);
127 s16 circle_pad_y = static_cast<s16>(circle_pad_y_f * MAX_CIRCLEPAD_POS); 126 s16 circle_pad_y = static_cast<s16>(circle_pad_y_f * MAX_CIRCLEPAD_POS);
128 state.hex |= GetCirclePadDirectionState(circle_pad_x, circle_pad_y).hex; 127 const DirectionState direction = GetStickDirectionState(circle_pad_x, circle_pad_y);
128 state.circle_up.Assign(direction.up);
129 state.circle_down.Assign(direction.down);
130 state.circle_left.Assign(direction.left);
131 state.circle_right.Assign(direction.right);
129 132
130 mem->pad.current_state.hex = state.hex; 133 mem->pad.current_state.hex = state.hex;
131 mem->pad.index = next_pad_index; 134 mem->pad.index = next_pad_index;
diff --git a/src/core/hle/service/hid/hid.h b/src/core/hle/service/hid/hid.h
index b505cdcd5..1ef972e70 100644
--- a/src/core/hle/service/hid/hid.h
+++ b/src/core/hle/service/hid/hid.h
@@ -176,6 +176,16 @@ ASSERT_REG_POSITION(touch.index_reset_ticks, 0x2A);
176#undef ASSERT_REG_POSITION 176#undef ASSERT_REG_POSITION
177#endif // !defined(_MSC_VER) 177#endif // !defined(_MSC_VER)
178 178
179struct DirectionState {
180 bool up;
181 bool down;
182 bool left;
183 bool right;
184};
185
186/// Translates analog stick axes to directions. This is exposed for ir_rst module to use.
187DirectionState GetStickDirectionState(s16 circle_pad_x, s16 circle_pad_y);
188
179/** 189/**
180 * HID::GetIPCHandles service function 190 * HID::GetIPCHandles service function
181 * Inputs: 191 * Inputs:
diff --git a/src/core/hle/service/ir/ir.cpp b/src/core/hle/service/ir/ir.cpp
index 7ac34a990..f06dd552f 100644
--- a/src/core/hle/service/ir/ir.cpp
+++ b/src/core/hle/service/ir/ir.cpp
@@ -25,6 +25,11 @@ void Shutdown() {
25 ShutdownRST(); 25 ShutdownRST();
26} 26}
27 27
28void ReloadInputDevices() {
29 ReloadInputDevicesUser();
30 ReloadInputDevicesRST();
31}
32
28} // namespace IR 33} // namespace IR
29 34
30} // namespace Service 35} // namespace Service
diff --git a/src/core/hle/service/ir/ir.h b/src/core/hle/service/ir/ir.h
index c741498e2..6be3e950c 100644
--- a/src/core/hle/service/ir/ir.h
+++ b/src/core/hle/service/ir/ir.h
@@ -16,5 +16,8 @@ void Init();
16/// Shutdown IR service 16/// Shutdown IR service
17void Shutdown(); 17void Shutdown();
18 18
19/// Reload input devices. Used when input configuration changed
20void ReloadInputDevices();
21
19} // namespace IR 22} // namespace IR
20} // namespace Service 23} // namespace Service
diff --git a/src/core/hle/service/ir/ir_rst.cpp b/src/core/hle/service/ir/ir_rst.cpp
index 3f1275c53..53807cd91 100644
--- a/src/core/hle/service/ir/ir_rst.cpp
+++ b/src/core/hle/service/ir/ir_rst.cpp
@@ -2,16 +2,135 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <atomic>
6#include "common/bit_field.h"
7#include "core/core_timing.h"
8#include "core/frontend/input.h"
5#include "core/hle/kernel/event.h" 9#include "core/hle/kernel/event.h"
6#include "core/hle/kernel/shared_memory.h" 10#include "core/hle/kernel/shared_memory.h"
11#include "core/hle/service/hid/hid.h"
7#include "core/hle/service/ir/ir.h" 12#include "core/hle/service/ir/ir.h"
8#include "core/hle/service/ir/ir_rst.h" 13#include "core/hle/service/ir/ir_rst.h"
14#include "core/settings.h"
9 15
10namespace Service { 16namespace Service {
11namespace IR { 17namespace IR {
12 18
13static Kernel::SharedPtr<Kernel::Event> handle_event; 19union PadState {
20 u32_le hex;
21
22 BitField<14, 1, u32_le> zl;
23 BitField<15, 1, u32_le> zr;
24
25 BitField<24, 1, u32_le> c_stick_right;
26 BitField<25, 1, u32_le> c_stick_left;
27 BitField<26, 1, u32_le> c_stick_up;
28 BitField<27, 1, u32_le> c_stick_down;
29};
30
31struct PadDataEntry {
32 PadState current_state;
33 PadState delta_additions;
34 PadState delta_removals;
35
36 s16_le c_stick_x;
37 s16_le c_stick_y;
38};
39
40struct SharedMem {
41 u64_le index_reset_ticks; ///< CPU tick count for when HID module updated entry index 0
42 u64_le index_reset_ticks_previous; ///< Previous `index_reset_ticks`
43 u32_le index;
44 INSERT_PADDING_WORDS(1);
45 std::array<PadDataEntry, 8> entries; ///< Last 8 pad entries
46};
47
48static_assert(sizeof(SharedMem) == 0x98, "SharedMem has wrong size!");
49
50static Kernel::SharedPtr<Kernel::Event> update_event;
14static Kernel::SharedPtr<Kernel::SharedMemory> shared_memory; 51static Kernel::SharedPtr<Kernel::SharedMemory> shared_memory;
52static u32 next_pad_index;
53static int update_callback_id;
54static std::unique_ptr<Input::ButtonDevice> zl_button;
55static std::unique_ptr<Input::ButtonDevice> zr_button;
56static std::unique_ptr<Input::AnalogDevice> c_stick;
57static std::atomic<bool> is_device_reload_pending;
58static bool raw_c_stick;
59static int update_period;
60
61static void LoadInputDevices() {
62 zl_button = Input::CreateDevice<Input::ButtonDevice>(
63 Settings::values.buttons[Settings::NativeButton::ZL]);
64 zr_button = Input::CreateDevice<Input::ButtonDevice>(
65 Settings::values.buttons[Settings::NativeButton::ZR]);
66 c_stick = Input::CreateDevice<Input::AnalogDevice>(
67 Settings::values.analogs[Settings::NativeAnalog::CStick]);
68}
69
70static void UnloadInputDevices() {
71 zl_button = nullptr;
72 zr_button = nullptr;
73 c_stick = nullptr;
74}
75
76static void UpdateCallback(u64 userdata, int cycles_late) {
77 SharedMem* mem = reinterpret_cast<SharedMem*>(shared_memory->GetPointer());
78
79 if (is_device_reload_pending.exchange(false))
80 LoadInputDevices();
81
82 PadState state;
83 state.zl.Assign(zl_button->GetStatus());
84 state.zr.Assign(zr_button->GetStatus());
85
86 // Get current c-stick position and update c-stick direction
87 float c_stick_x_f, c_stick_y_f;
88 std::tie(c_stick_x_f, c_stick_y_f) = c_stick->GetStatus();
89 constexpr int MAX_CSTICK_RADIUS = 0x9C; // Max value for a c-stick radius
90 const s16 c_stick_x = static_cast<s16>(c_stick_x_f * MAX_CSTICK_RADIUS);
91 const s16 c_stick_y = static_cast<s16>(c_stick_y_f * MAX_CSTICK_RADIUS);
92
93 if (!raw_c_stick) {
94 const HID::DirectionState direction = HID::GetStickDirectionState(c_stick_x, c_stick_y);
95 state.c_stick_up.Assign(direction.up);
96 state.c_stick_down.Assign(direction.down);
97 state.c_stick_left.Assign(direction.left);
98 state.c_stick_right.Assign(direction.right);
99 }
100
101 // TODO (wwylele): implement raw C-stick data for raw_c_stick = true
102
103 const u32 last_entry_index = mem->index;
104 mem->index = next_pad_index;
105 next_pad_index = (next_pad_index + 1) % mem->entries.size();
106
107 // Get the previous Pad state
108 PadState old_state{mem->entries[last_entry_index].current_state};
109
110 // Compute bitmask with 1s for bits different from the old state
111 PadState changed = {state.hex ^ old_state.hex};
112
113 // Get the current Pad entry
114 PadDataEntry& pad_entry = mem->entries[mem->index];
115
116 // Update entry properties
117 pad_entry.current_state.hex = state.hex;
118 pad_entry.delta_additions.hex = changed.hex & state.hex;
119 pad_entry.delta_removals.hex = changed.hex & old_state.hex;
120 pad_entry.c_stick_x = c_stick_x;
121 pad_entry.c_stick_y = c_stick_y;
122
123 // If we just updated index 0, provide a new timestamp
124 if (mem->index == 0) {
125 mem->index_reset_ticks_previous = mem->index_reset_ticks;
126 mem->index_reset_ticks = CoreTiming::GetTicks();
127 }
128
129 update_event->Signal();
130
131 // Reschedule recurrent event
132 CoreTiming::ScheduleEvent(msToCycles(update_period) - cycles_late, update_callback_id);
133}
15 134
16/** 135/**
17 * IR::GetHandles service function 136 * IR::GetHandles service function
@@ -22,18 +141,52 @@ static Kernel::SharedPtr<Kernel::SharedMemory> shared_memory;
22 * 4 : Event handle 141 * 4 : Event handle
23 */ 142 */
24static void GetHandles(Interface* self) { 143static void GetHandles(Interface* self) {
25 u32* cmd_buff = Kernel::GetCommandBuffer(); 144 IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x01, 0, 0);
145 IPC::RequestBuilder rb = rp.MakeBuilder(1, 3);
146 rb.Push(RESULT_SUCCESS);
147 rb.PushMoveHandles(Kernel::g_handle_table.Create(Service::IR::shared_memory).MoveFrom(),
148 Kernel::g_handle_table.Create(Service::IR::update_event).MoveFrom());
149}
150
151/**
152 * IR::Initialize service function
153 * Inputs:
154 * 1 : pad state update period in ms
155 * 2 : bool output raw c-stick data
156 */
157static void Initialize(Interface* self) {
158 IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x02, 2, 0);
159 update_period = static_cast<int>(rp.Pop<u32>());
160 raw_c_stick = rp.Pop<bool>();
26 161
27 cmd_buff[1] = RESULT_SUCCESS.raw; 162 if (raw_c_stick)
28 cmd_buff[2] = 0x4000000; 163 LOG_ERROR(Service_IR, "raw C-stick data is not implemented!");
29 cmd_buff[3] = Kernel::g_handle_table.Create(Service::IR::shared_memory).MoveFrom(); 164
30 cmd_buff[4] = Kernel::g_handle_table.Create(Service::IR::handle_event).MoveFrom(); 165 next_pad_index = 0;
166 is_device_reload_pending.store(true);
167 CoreTiming::ScheduleEvent(msToCycles(update_period), update_callback_id);
168
169 IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
170 rb.Push(RESULT_SUCCESS);
171
172 LOG_DEBUG(Service_IR, "called. update_period=%d, raw_c_stick=%d", update_period, raw_c_stick);
173}
174
175static void Shutdown(Interface* self) {
176 IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x03, 1, 0);
177
178 CoreTiming::UnscheduleEvent(update_callback_id, 0);
179 UnloadInputDevices();
180
181 IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
182 rb.Push(RESULT_SUCCESS);
183 LOG_DEBUG(Service_IR, "called");
31} 184}
32 185
33const Interface::FunctionInfo FunctionTable[] = { 186const Interface::FunctionInfo FunctionTable[] = {
34 {0x00010000, GetHandles, "GetHandles"}, 187 {0x00010000, GetHandles, "GetHandles"},
35 {0x00020080, nullptr, "Initialize"}, 188 {0x00020080, Initialize, "Initialize"},
36 {0x00030000, nullptr, "Shutdown"}, 189 {0x00030000, Shutdown, "Shutdown"},
37 {0x00090000, nullptr, "WriteToTwoFields"}, 190 {0x00090000, nullptr, "WriteToTwoFields"},
38}; 191};
39 192
@@ -43,17 +196,24 @@ IR_RST_Interface::IR_RST_Interface() {
43 196
44void InitRST() { 197void InitRST() {
45 using namespace Kernel; 198 using namespace Kernel;
46 199 // Note: these two kernel objects are even available before Initialize service function is
200 // called.
47 shared_memory = 201 shared_memory =
48 SharedMemory::Create(nullptr, 0x1000, MemoryPermission::ReadWrite, 202 SharedMemory::Create(nullptr, 0x1000, MemoryPermission::ReadWrite, MemoryPermission::Read,
49 MemoryPermission::ReadWrite, 0, MemoryRegion::BASE, "IR:SharedMemory"); 203 0, MemoryRegion::BASE, "IRRST:SharedMemory");
204 update_event = Event::Create(ResetType::OneShot, "IRRST:UpdateEvent");
50 205
51 handle_event = Event::Create(ResetType::OneShot, "IR:HandleEvent"); 206 update_callback_id = CoreTiming::RegisterEvent("IRRST:UpdateCallBack", UpdateCallback);
52} 207}
53 208
54void ShutdownRST() { 209void ShutdownRST() {
55 shared_memory = nullptr; 210 shared_memory = nullptr;
56 handle_event = nullptr; 211 update_event = nullptr;
212 UnloadInputDevices();
213}
214
215void ReloadInputDevicesRST() {
216 is_device_reload_pending.store(true);
57} 217}
58 218
59} // namespace IR 219} // namespace IR
diff --git a/src/core/hle/service/ir/ir_rst.h b/src/core/hle/service/ir/ir_rst.h
index 75b732627..d932bb7e5 100644
--- a/src/core/hle/service/ir/ir_rst.h
+++ b/src/core/hle/service/ir/ir_rst.h
@@ -21,5 +21,8 @@ public:
21void InitRST(); 21void InitRST();
22void ShutdownRST(); 22void ShutdownRST();
23 23
24/// Reload input devices. Used when input configuration changed
25void ReloadInputDevicesRST();
26
24} // namespace IR 27} // namespace IR
25} // namespace Service 28} // namespace Service
diff --git a/src/core/hle/service/ir/ir_user.cpp b/src/core/hle/service/ir/ir_user.cpp
index bccf6bce7..226af0083 100644
--- a/src/core/hle/service/ir/ir_user.cpp
+++ b/src/core/hle/service/ir/ir_user.cpp
@@ -542,7 +542,7 @@ void ShutdownUser() {
542 receive_event = nullptr; 542 receive_event = nullptr;
543} 543}
544 544
545void ReloadInputDevices() { 545void ReloadInputDevicesUser() {
546 if (extra_hid) 546 if (extra_hid)
547 extra_hid->RequestInputDevicesReload(); 547 extra_hid->RequestInputDevicesReload();
548} 548}
diff --git a/src/core/hle/service/ir/ir_user.h b/src/core/hle/service/ir/ir_user.h
index 2401346e8..930650406 100644
--- a/src/core/hle/service/ir/ir_user.h
+++ b/src/core/hle/service/ir/ir_user.h
@@ -52,7 +52,7 @@ void InitUser();
52void ShutdownUser(); 52void ShutdownUser();
53 53
54/// Reload input devices. Used when input configuration changed 54/// Reload input devices. Used when input configuration changed
55void ReloadInputDevices(); 55void ReloadInputDevicesUser();
56 56
57} // namespace IR 57} // namespace IR
58} // namespace Service 58} // namespace Service
diff --git a/src/core/settings.cpp b/src/core/settings.cpp
index 3d22c0afa..d2e7c6b97 100644
--- a/src/core/settings.cpp
+++ b/src/core/settings.cpp
@@ -5,7 +5,7 @@
5#include "audio_core/audio_core.h" 5#include "audio_core/audio_core.h"
6#include "core/gdbstub/gdbstub.h" 6#include "core/gdbstub/gdbstub.h"
7#include "core/hle/service/hid/hid.h" 7#include "core/hle/service/hid/hid.h"
8#include "core/hle/service/ir/ir_user.h" 8#include "core/hle/service/ir/ir.h"
9#include "settings.h" 9#include "settings.h"
10#include "video_core/video_core.h" 10#include "video_core/video_core.h"
11 11