summaryrefslogtreecommitdiff
path: root/src/core/hid/emulated_console.cpp
diff options
context:
space:
mode:
authorGravatar Narr the Reg2022-11-20 09:31:20 -0600
committerGravatar GitHub2022-11-20 09:31:20 -0600
commitdb7bcd51ae09c4ef25e08096de563903f61e2380 (patch)
tree5ae9977b48e1aff118fae3ebffb215b0b4afa887 /src/core/hid/emulated_console.cpp
parentservice: nfc: Implement nfc user (diff)
parentMerge pull request #9238 from german77/cabinet_applet (diff)
downloadyuzu-db7bcd51ae09c4ef25e08096de563903f61e2380.tar.gz
yuzu-db7bcd51ae09c4ef25e08096de563903f61e2380.tar.xz
yuzu-db7bcd51ae09c4ef25e08096de563903f61e2380.zip
Merge branch 'master' into nfc_impl
Diffstat (limited to 'src/core/hid/emulated_console.cpp')
-rw-r--r--src/core/hid/emulated_console.cpp94
1 files changed, 72 insertions, 22 deletions
diff --git a/src/core/hid/emulated_console.cpp b/src/core/hid/emulated_console.cpp
index aac45907d..fb7e5802a 100644
--- a/src/core/hid/emulated_console.cpp
+++ b/src/core/hid/emulated_console.cpp
@@ -19,27 +19,26 @@ void EmulatedConsole::ReloadFromSettings() {
19} 19}
20 20
21void EmulatedConsole::SetTouchParams() { 21void EmulatedConsole::SetTouchParams() {
22 // TODO(german77): Support any number of fingers
23 std::size_t index = 0; 22 std::size_t index = 0;
24 23
25 // Hardcode mouse, touchscreen and cemuhook parameters 24 // We can't use mouse as touch if native mouse is enabled
26 if (!Settings::values.mouse_enabled) { 25 if (!Settings::values.mouse_enabled) {
27 // We can't use mouse as touch if native mouse is enabled
28 touch_params[index++] = Common::ParamPackage{"engine:mouse,axis_x:10,axis_y:11,button:0"}; 26 touch_params[index++] = Common::ParamPackage{"engine:mouse,axis_x:10,axis_y:11,button:0"};
29 } 27 }
30 28
31 touch_params[index++] = 29 touch_params[index++] =
32 Common::ParamPackage{"engine:touch,axis_x:0,axis_y:1,button:0,touch_id:0"}; 30 Common::ParamPackage{"engine:cemuhookudp,axis_x:17,axis_y:18,button:65536"};
33 touch_params[index++] = 31 touch_params[index++] =
34 Common::ParamPackage{"engine:touch,axis_x:2,axis_y:3,button:1,touch_id:1"}; 32 Common::ParamPackage{"engine:cemuhookudp,axis_x:19,axis_y:20,button:131072"};
35 touch_params[index++] = 33
36 Common::ParamPackage{"engine:touch,axis_x:4,axis_y:5,button:2,touch_id:2"}; 34 for (int i = 0; i < static_cast<int>(MaxActiveTouchInputs); i++) {
37 touch_params[index++] = 35 Common::ParamPackage touchscreen_param{};
38 Common::ParamPackage{"engine:touch,axis_x:6,axis_y:7,button:3,touch_id:3"}; 36 touchscreen_param.Set("engine", "touch");
39 touch_params[index++] = 37 touchscreen_param.Set("axis_x", i * 2);
40 Common::ParamPackage{"engine:cemuhookudp,axis_x:17,axis_y:18,button:65536,touch_id:0"}; 38 touchscreen_param.Set("axis_y", (i * 2) + 1);
41 touch_params[index++] = 39 touchscreen_param.Set("button", i);
42 Common::ParamPackage{"engine:cemuhookudp,axis_x:19,axis_y:20,button:131072,touch_id:1"}; 40 touch_params[index++] = touchscreen_param;
41 }
43 42
44 const auto button_index = 43 const auto button_index =
45 static_cast<u64>(Settings::values.touch_from_button_map_index.GetValue()); 44 static_cast<u64>(Settings::values.touch_from_button_map_index.GetValue());
@@ -47,7 +46,7 @@ void EmulatedConsole::SetTouchParams() {
47 46
48 // Map the rest of the fingers from touch from button configuration 47 // Map the rest of the fingers from touch from button configuration
49 for (const auto& config_entry : touch_buttons) { 48 for (const auto& config_entry : touch_buttons) {
50 if (index >= touch_params.size()) { 49 if (index >= MaxTouchDevices) {
51 continue; 50 continue;
52 } 51 }
53 Common::ParamPackage params{config_entry}; 52 Common::ParamPackage params{config_entry};
@@ -60,7 +59,6 @@ void EmulatedConsole::SetTouchParams() {
60 touch_button_params.Set("button", params.Serialize()); 59 touch_button_params.Set("button", params.Serialize());
61 touch_button_params.Set("x", x); 60 touch_button_params.Set("x", x);
62 touch_button_params.Set("y", y); 61 touch_button_params.Set("y", y);
63 touch_button_params.Set("touch_id", static_cast<int>(index));
64 touch_params[index] = touch_button_params; 62 touch_params[index] = touch_button_params;
65 index++; 63 index++;
66 } 64 }
@@ -178,12 +176,38 @@ void EmulatedConsole::SetMotion(const Common::Input::CallbackStatus& callback) {
178} 176}
179 177
180void EmulatedConsole::SetTouch(const Common::Input::CallbackStatus& callback, std::size_t index) { 178void EmulatedConsole::SetTouch(const Common::Input::CallbackStatus& callback, std::size_t index) {
181 if (index >= console.touch_values.size()) { 179 if (index >= MaxTouchDevices) {
182 return; 180 return;
183 } 181 }
184 std::unique_lock lock{mutex}; 182 std::unique_lock lock{mutex};
185 183
186 console.touch_values[index] = TransformToTouch(callback); 184 const auto touch_input = TransformToTouch(callback);
185 auto touch_index = GetIndexFromFingerId(index);
186 bool is_new_input = false;
187
188 if (!touch_index.has_value() && touch_input.pressed.value) {
189 touch_index = GetNextFreeIndex();
190 is_new_input = true;
191 }
192
193 // No free entries or invalid state. Ignore input
194 if (!touch_index.has_value()) {
195 return;
196 }
197
198 auto& touch_value = console.touch_values[touch_index.value()];
199
200 if (is_new_input) {
201 touch_value.pressed.value = true;
202 touch_value.id = static_cast<u32>(index);
203 }
204
205 touch_value.x = touch_input.x;
206 touch_value.y = touch_input.y;
207
208 if (!touch_input.pressed.value) {
209 touch_value.pressed.value = false;
210 }
187 211
188 if (is_configuring) { 212 if (is_configuring) {
189 lock.unlock(); 213 lock.unlock();
@@ -191,11 +215,15 @@ void EmulatedConsole::SetTouch(const Common::Input::CallbackStatus& callback, st
191 return; 215 return;
192 } 216 }
193 217
194 // TODO(german77): Remap touch id in sequential order 218 // Touch outside allowed range. Ignore input
195 console.touch_state[index] = { 219 if (touch_index.value() >= MaxActiveTouchInputs) {
196 .position = {console.touch_values[index].x.value, console.touch_values[index].y.value}, 220 return;
197 .id = static_cast<u32>(console.touch_values[index].id), 221 }
198 .pressed = console.touch_values[index].pressed.value, 222
223 console.touch_state[touch_index.value()] = {
224 .position = {touch_value.x.value, touch_value.y.value},
225 .id = static_cast<u32>(touch_index.value()),
226 .pressed = touch_input.pressed.value,
199 }; 227 };
200 228
201 lock.unlock(); 229 lock.unlock();
@@ -222,6 +250,28 @@ TouchFingerState EmulatedConsole::GetTouch() const {
222 return console.touch_state; 250 return console.touch_state;
223} 251}
224 252
253std::optional<std::size_t> EmulatedConsole::GetIndexFromFingerId(std::size_t finger_id) const {
254 for (std::size_t index = 0; index < MaxTouchDevices; ++index) {
255 const auto& finger = console.touch_values[index];
256 if (!finger.pressed.value) {
257 continue;
258 }
259 if (finger.id == static_cast<int>(finger_id)) {
260 return index;
261 }
262 }
263 return std::nullopt;
264}
265
266std::optional<std::size_t> EmulatedConsole::GetNextFreeIndex() const {
267 for (std::size_t index = 0; index < MaxTouchDevices; ++index) {
268 if (!console.touch_values[index].pressed.value) {
269 return index;
270 }
271 }
272 return std::nullopt;
273}
274
225void EmulatedConsole::TriggerOnChange(ConsoleTriggerType type) { 275void EmulatedConsole::TriggerOnChange(ConsoleTriggerType type) {
226 std::scoped_lock lock{callback_mutex}; 276 std::scoped_lock lock{callback_mutex};
227 for (const auto& poller_pair : callback_list) { 277 for (const auto& poller_pair : callback_list) {