summaryrefslogtreecommitdiff
path: root/src/input_common/input_mapping.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/input_mapping.cpp')
-rw-r--r--src/input_common/input_mapping.cpp171
1 files changed, 171 insertions, 0 deletions
diff --git a/src/input_common/input_mapping.cpp b/src/input_common/input_mapping.cpp
new file mode 100644
index 000000000..0ffc71028
--- /dev/null
+++ b/src/input_common/input_mapping.cpp
@@ -0,0 +1,171 @@
1// Copyright 2021 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included
4
5#include "common/common_types.h"
6#include "input_common/input_engine.h"
7#include "input_common/input_mapping.h"
8
9namespace InputCommon {
10
11MappingFactory::MappingFactory() {}
12
13void MappingFactory::BeginMapping(Polling::InputType type) {
14 is_enabled = true;
15 input_type = type;
16 input_queue.Clear();
17 first_axis = -1;
18 second_axis = -1;
19}
20
21[[nodiscard]] const Common::ParamPackage MappingFactory::GetNextInput() {
22 Common::ParamPackage input;
23 input_queue.Pop(input);
24 return input;
25}
26
27void MappingFactory::RegisterInput(const MappingData& data) {
28 if (!is_enabled) {
29 return;
30 }
31 switch (input_type) {
32 case Polling::InputType::Button:
33 RegisterButton(data);
34 return;
35 case Polling::InputType::Stick:
36 RegisterStick(data);
37 return;
38 case Polling::InputType::Motion:
39 RegisterMotion(data);
40 return;
41 default:
42 return;
43 }
44}
45
46void MappingFactory::StopMapping() {
47 is_enabled = false;
48 input_type = Polling::InputType::None;
49 input_queue.Clear();
50}
51
52void MappingFactory::RegisterButton(const MappingData& data) {
53 Common::ParamPackage new_input;
54 new_input.Set("engine", data.engine);
55 if (data.pad.guid != Common::UUID{}) {
56 new_input.Set("guid", data.pad.guid.Format());
57 }
58 new_input.Set("port", static_cast<int>(data.pad.port));
59 new_input.Set("pad", static_cast<int>(data.pad.pad));
60 switch (data.type) {
61 case EngineInputType::Button:
62 // Workaround for old compatibility
63 if (data.engine == "keyboard") {
64 new_input.Set("code", data.index);
65 break;
66 }
67 new_input.Set("button", data.index);
68 break;
69 case EngineInputType::HatButton:
70 new_input.Set("hat", data.index);
71 new_input.Set("direction", data.hat_name);
72 break;
73 case EngineInputType::Analog:
74 new_input.Set("axis", data.index);
75 new_input.Set("threshold", 0.5f);
76 break;
77 default:
78 return;
79 }
80 input_queue.Push(new_input);
81}
82
83void MappingFactory::RegisterStick(const MappingData& data) {
84 Common::ParamPackage new_input;
85 new_input.Set("engine", data.engine);
86 if (data.pad.guid != Common::UUID{}) {
87 new_input.Set("guid", data.pad.guid.Format());
88 }
89 new_input.Set("port", static_cast<int>(data.pad.port));
90 new_input.Set("pad", static_cast<int>(data.pad.pad));
91
92 // If engine is mouse map the mouse position as a joystick
93 if (data.engine == "mouse") {
94 new_input.Set("axis_x", 0);
95 new_input.Set("axis_y", 1);
96 new_input.Set("threshold", 0.5f);
97 new_input.Set("range", 1.0f);
98 new_input.Set("deadzone", 0.0f);
99 input_queue.Push(new_input);
100 return;
101 }
102
103 switch (data.type) {
104 case EngineInputType::Button:
105 case EngineInputType::HatButton:
106 RegisterButton(data);
107 return;
108 case EngineInputType::Analog:
109 if (first_axis == data.index) {
110 return;
111 }
112 if (first_axis == -1) {
113 first_axis = data.index;
114 return;
115 }
116 new_input.Set("axis_x", first_axis);
117 new_input.Set("axis_y", data.index);
118 new_input.Set("threshold", 0.5f);
119 new_input.Set("range", 0.95f);
120 new_input.Set("deadzone", 0.15f);
121 break;
122 default:
123 return;
124 }
125 input_queue.Push(new_input);
126}
127
128void MappingFactory::RegisterMotion(const MappingData& data) {
129 Common::ParamPackage new_input;
130 new_input.Set("engine", data.engine);
131 if (data.pad.guid != Common::UUID{}) {
132 new_input.Set("guid", data.pad.guid.Format());
133 }
134 new_input.Set("port", static_cast<int>(data.pad.port));
135 new_input.Set("pad", static_cast<int>(data.pad.pad));
136 switch (data.type) {
137 case EngineInputType::Button:
138 case EngineInputType::HatButton:
139 RegisterButton(data);
140 return;
141 case EngineInputType::Analog:
142 if (first_axis == data.index) {
143 return;
144 }
145 if (second_axis == data.index) {
146 return;
147 }
148 if (first_axis == -1) {
149 first_axis = data.index;
150 return;
151 }
152 if (second_axis == -1) {
153 second_axis = data.index;
154 return;
155 }
156 new_input.Set("axis_x", first_axis);
157 new_input.Set("axis_y", second_axis);
158 new_input.Set("axis_z", data.index);
159 new_input.Set("range", 1.0f);
160 new_input.Set("deadzone", 0.20f);
161 break;
162 case EngineInputType::Motion:
163 new_input.Set("motion", data.index);
164 break;
165 default:
166 return;
167 }
168 input_queue.Push(new_input);
169}
170
171} // namespace InputCommon