input_poller.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/common_types.h"
  4. #include "common/input.h"
  5. #include "input_common/input_engine.h"
  6. #include "input_common/input_poller.h"
  7. namespace InputCommon {
  8. class DummyInput final : public Common::Input::InputDevice {
  9. public:
  10. explicit DummyInput() = default;
  11. };
  12. class InputFromButton final : public Common::Input::InputDevice {
  13. public:
  14. explicit InputFromButton(PadIdentifier identifier_, int button_, bool toggle_, bool inverted_,
  15. InputEngine* input_engine_)
  16. : identifier(identifier_), button(button_), toggle(toggle_), inverted(inverted_),
  17. input_engine(input_engine_) {
  18. UpdateCallback engine_callback{[this]() { OnChange(); }};
  19. const InputIdentifier input_identifier{
  20. .identifier = identifier,
  21. .type = EngineInputType::Button,
  22. .index = button,
  23. .callback = engine_callback,
  24. };
  25. last_button_value = false;
  26. callback_key = input_engine->SetCallback(input_identifier);
  27. }
  28. ~InputFromButton() override {
  29. input_engine->DeleteCallback(callback_key);
  30. }
  31. Common::Input::ButtonStatus GetStatus() const {
  32. return {
  33. .value = input_engine->GetButton(identifier, button),
  34. .inverted = inverted,
  35. .toggle = toggle,
  36. };
  37. }
  38. void ForceUpdate() override {
  39. const Common::Input::CallbackStatus status{
  40. .type = Common::Input::InputType::Button,
  41. .button_status = GetStatus(),
  42. };
  43. last_button_value = status.button_status.value;
  44. TriggerOnChange(status);
  45. }
  46. void OnChange() {
  47. const Common::Input::CallbackStatus status{
  48. .type = Common::Input::InputType::Button,
  49. .button_status = GetStatus(),
  50. };
  51. if (status.button_status.value != last_button_value) {
  52. last_button_value = status.button_status.value;
  53. TriggerOnChange(status);
  54. }
  55. }
  56. private:
  57. const PadIdentifier identifier;
  58. const int button;
  59. const bool toggle;
  60. const bool inverted;
  61. int callback_key;
  62. bool last_button_value;
  63. InputEngine* input_engine;
  64. };
  65. class InputFromHatButton final : public Common::Input::InputDevice {
  66. public:
  67. explicit InputFromHatButton(PadIdentifier identifier_, int button_, u8 direction_, bool toggle_,
  68. bool inverted_, InputEngine* input_engine_)
  69. : identifier(identifier_), button(button_), direction(direction_), toggle(toggle_),
  70. inverted(inverted_), input_engine(input_engine_) {
  71. UpdateCallback engine_callback{[this]() { OnChange(); }};
  72. const InputIdentifier input_identifier{
  73. .identifier = identifier,
  74. .type = EngineInputType::HatButton,
  75. .index = button,
  76. .callback = engine_callback,
  77. };
  78. last_button_value = false;
  79. callback_key = input_engine->SetCallback(input_identifier);
  80. }
  81. ~InputFromHatButton() override {
  82. input_engine->DeleteCallback(callback_key);
  83. }
  84. Common::Input::ButtonStatus GetStatus() const {
  85. return {
  86. .value = input_engine->GetHatButton(identifier, button, direction),
  87. .inverted = inverted,
  88. .toggle = toggle,
  89. };
  90. }
  91. void ForceUpdate() override {
  92. const Common::Input::CallbackStatus status{
  93. .type = Common::Input::InputType::Button,
  94. .button_status = GetStatus(),
  95. };
  96. last_button_value = status.button_status.value;
  97. TriggerOnChange(status);
  98. }
  99. void OnChange() {
  100. const Common::Input::CallbackStatus status{
  101. .type = Common::Input::InputType::Button,
  102. .button_status = GetStatus(),
  103. };
  104. if (status.button_status.value != last_button_value) {
  105. last_button_value = status.button_status.value;
  106. TriggerOnChange(status);
  107. }
  108. }
  109. private:
  110. const PadIdentifier identifier;
  111. const int button;
  112. const u8 direction;
  113. const bool toggle;
  114. const bool inverted;
  115. int callback_key;
  116. bool last_button_value;
  117. InputEngine* input_engine;
  118. };
  119. class InputFromStick final : public Common::Input::InputDevice {
  120. public:
  121. explicit InputFromStick(PadIdentifier identifier_, int axis_x_, int axis_y_,
  122. Common::Input::AnalogProperties properties_x_,
  123. Common::Input::AnalogProperties properties_y_,
  124. InputEngine* input_engine_)
  125. : identifier(identifier_), axis_x(axis_x_), axis_y(axis_y_), properties_x(properties_x_),
  126. properties_y(properties_y_),
  127. input_engine(input_engine_), invert_axis_y{input_engine_->GetEngineName() == "sdl"} {
  128. UpdateCallback engine_callback{[this]() { OnChange(); }};
  129. const InputIdentifier x_input_identifier{
  130. .identifier = identifier,
  131. .type = EngineInputType::Analog,
  132. .index = axis_x,
  133. .callback = engine_callback,
  134. };
  135. const InputIdentifier y_input_identifier{
  136. .identifier = identifier,
  137. .type = EngineInputType::Analog,
  138. .index = axis_y,
  139. .callback = engine_callback,
  140. };
  141. last_axis_x_value = 0.0f;
  142. last_axis_y_value = 0.0f;
  143. callback_key_x = input_engine->SetCallback(x_input_identifier);
  144. callback_key_y = input_engine->SetCallback(y_input_identifier);
  145. }
  146. ~InputFromStick() override {
  147. input_engine->DeleteCallback(callback_key_x);
  148. input_engine->DeleteCallback(callback_key_y);
  149. }
  150. Common::Input::StickStatus GetStatus() const {
  151. Common::Input::StickStatus status;
  152. status.x = {
  153. .raw_value = input_engine->GetAxis(identifier, axis_x),
  154. .properties = properties_x,
  155. };
  156. status.y = {
  157. .raw_value = input_engine->GetAxis(identifier, axis_y),
  158. .properties = properties_y,
  159. };
  160. // This is a workaround to keep compatibility with old yuzu versions. Vertical axis is
  161. // inverted on SDL compared to Nintendo
  162. if (invert_axis_y) {
  163. status.y.raw_value = -status.y.raw_value;
  164. }
  165. return status;
  166. }
  167. void ForceUpdate() override {
  168. const Common::Input::CallbackStatus status{
  169. .type = Common::Input::InputType::Stick,
  170. .stick_status = GetStatus(),
  171. };
  172. last_axis_x_value = status.stick_status.x.raw_value;
  173. last_axis_y_value = status.stick_status.y.raw_value;
  174. TriggerOnChange(status);
  175. }
  176. void OnChange() {
  177. const Common::Input::CallbackStatus status{
  178. .type = Common::Input::InputType::Stick,
  179. .stick_status = GetStatus(),
  180. };
  181. if (status.stick_status.x.raw_value != last_axis_x_value ||
  182. status.stick_status.y.raw_value != last_axis_y_value) {
  183. last_axis_x_value = status.stick_status.x.raw_value;
  184. last_axis_y_value = status.stick_status.y.raw_value;
  185. TriggerOnChange(status);
  186. }
  187. }
  188. private:
  189. const PadIdentifier identifier;
  190. const int axis_x;
  191. const int axis_y;
  192. const Common::Input::AnalogProperties properties_x;
  193. const Common::Input::AnalogProperties properties_y;
  194. int callback_key_x;
  195. int callback_key_y;
  196. float last_axis_x_value;
  197. float last_axis_y_value;
  198. InputEngine* input_engine;
  199. const bool invert_axis_y;
  200. };
  201. class InputFromTouch final : public Common::Input::InputDevice {
  202. public:
  203. explicit InputFromTouch(PadIdentifier identifier_, int button_, bool toggle_, bool inverted_,
  204. int axis_x_, int axis_y_, Common::Input::AnalogProperties properties_x_,
  205. Common::Input::AnalogProperties properties_y_,
  206. InputEngine* input_engine_)
  207. : identifier(identifier_), button(button_), toggle(toggle_), inverted(inverted_),
  208. axis_x(axis_x_), axis_y(axis_y_), properties_x(properties_x_),
  209. properties_y(properties_y_), input_engine(input_engine_) {
  210. UpdateCallback engine_callback{[this]() { OnChange(); }};
  211. const InputIdentifier button_input_identifier{
  212. .identifier = identifier,
  213. .type = EngineInputType::Button,
  214. .index = button,
  215. .callback = engine_callback,
  216. };
  217. const InputIdentifier x_input_identifier{
  218. .identifier = identifier,
  219. .type = EngineInputType::Analog,
  220. .index = axis_x,
  221. .callback = engine_callback,
  222. };
  223. const InputIdentifier y_input_identifier{
  224. .identifier = identifier,
  225. .type = EngineInputType::Analog,
  226. .index = axis_y,
  227. .callback = engine_callback,
  228. };
  229. last_axis_x_value = 0.0f;
  230. last_axis_y_value = 0.0f;
  231. last_button_value = false;
  232. callback_key_button = input_engine->SetCallback(button_input_identifier);
  233. callback_key_x = input_engine->SetCallback(x_input_identifier);
  234. callback_key_y = input_engine->SetCallback(y_input_identifier);
  235. }
  236. ~InputFromTouch() override {
  237. input_engine->DeleteCallback(callback_key_button);
  238. input_engine->DeleteCallback(callback_key_x);
  239. input_engine->DeleteCallback(callback_key_y);
  240. }
  241. Common::Input::TouchStatus GetStatus() const {
  242. Common::Input::TouchStatus status{};
  243. status.pressed = {
  244. .value = input_engine->GetButton(identifier, button),
  245. .inverted = inverted,
  246. .toggle = toggle,
  247. };
  248. status.x = {
  249. .raw_value = input_engine->GetAxis(identifier, axis_x),
  250. .properties = properties_x,
  251. };
  252. status.y = {
  253. .raw_value = input_engine->GetAxis(identifier, axis_y),
  254. .properties = properties_y,
  255. };
  256. return status;
  257. }
  258. void OnChange() {
  259. const Common::Input::CallbackStatus status{
  260. .type = Common::Input::InputType::Touch,
  261. .touch_status = GetStatus(),
  262. };
  263. if (status.touch_status.x.raw_value != last_axis_x_value ||
  264. status.touch_status.y.raw_value != last_axis_y_value ||
  265. status.touch_status.pressed.value != last_button_value) {
  266. last_axis_x_value = status.touch_status.x.raw_value;
  267. last_axis_y_value = status.touch_status.y.raw_value;
  268. last_button_value = status.touch_status.pressed.value;
  269. TriggerOnChange(status);
  270. }
  271. }
  272. private:
  273. const PadIdentifier identifier;
  274. const int button;
  275. const bool toggle;
  276. const bool inverted;
  277. const int axis_x;
  278. const int axis_y;
  279. const Common::Input::AnalogProperties properties_x;
  280. const Common::Input::AnalogProperties properties_y;
  281. int callback_key_button;
  282. int callback_key_x;
  283. int callback_key_y;
  284. bool last_button_value;
  285. float last_axis_x_value;
  286. float last_axis_y_value;
  287. InputEngine* input_engine;
  288. };
  289. class InputFromTrigger final : public Common::Input::InputDevice {
  290. public:
  291. explicit InputFromTrigger(PadIdentifier identifier_, int button_, bool toggle_, bool inverted_,
  292. int axis_, Common::Input::AnalogProperties properties_,
  293. InputEngine* input_engine_)
  294. : identifier(identifier_), button(button_), toggle(toggle_), inverted(inverted_),
  295. axis(axis_), properties(properties_), input_engine(input_engine_) {
  296. UpdateCallback engine_callback{[this]() { OnChange(); }};
  297. const InputIdentifier button_input_identifier{
  298. .identifier = identifier,
  299. .type = EngineInputType::Button,
  300. .index = button,
  301. .callback = engine_callback,
  302. };
  303. const InputIdentifier axis_input_identifier{
  304. .identifier = identifier,
  305. .type = EngineInputType::Analog,
  306. .index = axis,
  307. .callback = engine_callback,
  308. };
  309. last_axis_value = 0.0f;
  310. last_button_value = false;
  311. callback_key_button = input_engine->SetCallback(button_input_identifier);
  312. axis_callback_key = input_engine->SetCallback(axis_input_identifier);
  313. }
  314. ~InputFromTrigger() override {
  315. input_engine->DeleteCallback(callback_key_button);
  316. input_engine->DeleteCallback(axis_callback_key);
  317. }
  318. Common::Input::TriggerStatus GetStatus() const {
  319. const Common::Input::AnalogStatus analog_status{
  320. .raw_value = input_engine->GetAxis(identifier, axis),
  321. .properties = properties,
  322. };
  323. const Common::Input::ButtonStatus button_status{
  324. .value = input_engine->GetButton(identifier, button),
  325. .inverted = inverted,
  326. .toggle = toggle,
  327. };
  328. return {
  329. .analog = analog_status,
  330. .pressed = button_status,
  331. };
  332. }
  333. void OnChange() {
  334. const Common::Input::CallbackStatus status{
  335. .type = Common::Input::InputType::Trigger,
  336. .trigger_status = GetStatus(),
  337. };
  338. if (status.trigger_status.analog.raw_value != last_axis_value ||
  339. status.trigger_status.pressed.value != last_button_value) {
  340. last_axis_value = status.trigger_status.analog.raw_value;
  341. last_button_value = status.trigger_status.pressed.value;
  342. TriggerOnChange(status);
  343. }
  344. }
  345. private:
  346. const PadIdentifier identifier;
  347. const int button;
  348. const bool toggle;
  349. const bool inverted;
  350. const int axis;
  351. const Common::Input::AnalogProperties properties;
  352. int callback_key_button;
  353. int axis_callback_key;
  354. bool last_button_value;
  355. float last_axis_value;
  356. InputEngine* input_engine;
  357. };
  358. class InputFromAnalog final : public Common::Input::InputDevice {
  359. public:
  360. explicit InputFromAnalog(PadIdentifier identifier_, int axis_,
  361. Common::Input::AnalogProperties properties_,
  362. InputEngine* input_engine_)
  363. : identifier(identifier_), axis(axis_), properties(properties_),
  364. input_engine(input_engine_) {
  365. UpdateCallback engine_callback{[this]() { OnChange(); }};
  366. const InputIdentifier input_identifier{
  367. .identifier = identifier,
  368. .type = EngineInputType::Analog,
  369. .index = axis,
  370. .callback = engine_callback,
  371. };
  372. last_axis_value = 0.0f;
  373. callback_key = input_engine->SetCallback(input_identifier);
  374. }
  375. ~InputFromAnalog() override {
  376. input_engine->DeleteCallback(callback_key);
  377. }
  378. Common::Input::AnalogStatus GetStatus() const {
  379. return {
  380. .raw_value = input_engine->GetAxis(identifier, axis),
  381. .properties = properties,
  382. };
  383. }
  384. void OnChange() {
  385. const Common::Input::CallbackStatus status{
  386. .type = Common::Input::InputType::Analog,
  387. .analog_status = GetStatus(),
  388. };
  389. if (status.analog_status.raw_value != last_axis_value) {
  390. last_axis_value = status.analog_status.raw_value;
  391. TriggerOnChange(status);
  392. }
  393. }
  394. private:
  395. const PadIdentifier identifier;
  396. const int axis;
  397. const Common::Input::AnalogProperties properties;
  398. int callback_key;
  399. float last_axis_value;
  400. InputEngine* input_engine;
  401. };
  402. class InputFromBattery final : public Common::Input::InputDevice {
  403. public:
  404. explicit InputFromBattery(PadIdentifier identifier_, InputEngine* input_engine_)
  405. : identifier(identifier_), input_engine(input_engine_) {
  406. UpdateCallback engine_callback{[this]() { OnChange(); }};
  407. const InputIdentifier input_identifier{
  408. .identifier = identifier,
  409. .type = EngineInputType::Battery,
  410. .index = 0,
  411. .callback = engine_callback,
  412. };
  413. last_battery_value = Common::Input::BatteryStatus::Charging;
  414. callback_key = input_engine->SetCallback(input_identifier);
  415. }
  416. ~InputFromBattery() override {
  417. input_engine->DeleteCallback(callback_key);
  418. }
  419. Common::Input::BatteryStatus GetStatus() const {
  420. return input_engine->GetBattery(identifier);
  421. }
  422. void ForceUpdate() override {
  423. const Common::Input::CallbackStatus status{
  424. .type = Common::Input::InputType::Battery,
  425. .battery_status = GetStatus(),
  426. };
  427. last_battery_value = status.battery_status;
  428. TriggerOnChange(status);
  429. }
  430. void OnChange() {
  431. const Common::Input::CallbackStatus status{
  432. .type = Common::Input::InputType::Battery,
  433. .battery_status = GetStatus(),
  434. };
  435. if (status.battery_status != last_battery_value) {
  436. last_battery_value = status.battery_status;
  437. TriggerOnChange(status);
  438. }
  439. }
  440. private:
  441. const PadIdentifier identifier;
  442. int callback_key;
  443. Common::Input::BatteryStatus last_battery_value;
  444. InputEngine* input_engine;
  445. };
  446. class InputFromMotion final : public Common::Input::InputDevice {
  447. public:
  448. explicit InputFromMotion(PadIdentifier identifier_, int motion_sensor_, float gyro_threshold_,
  449. InputEngine* input_engine_)
  450. : identifier(identifier_), motion_sensor(motion_sensor_), gyro_threshold(gyro_threshold_),
  451. input_engine(input_engine_) {
  452. UpdateCallback engine_callback{[this]() { OnChange(); }};
  453. const InputIdentifier input_identifier{
  454. .identifier = identifier,
  455. .type = EngineInputType::Motion,
  456. .index = motion_sensor,
  457. .callback = engine_callback,
  458. };
  459. callback_key = input_engine->SetCallback(input_identifier);
  460. }
  461. ~InputFromMotion() override {
  462. input_engine->DeleteCallback(callback_key);
  463. }
  464. Common::Input::MotionStatus GetStatus() const {
  465. const auto basic_motion = input_engine->GetMotion(identifier, motion_sensor);
  466. Common::Input::MotionStatus status{};
  467. const Common::Input::AnalogProperties properties = {
  468. .deadzone = 0.0f,
  469. .range = 1.0f,
  470. .threshold = gyro_threshold,
  471. .offset = 0.0f,
  472. };
  473. status.accel.x = {.raw_value = basic_motion.accel_x, .properties = properties};
  474. status.accel.y = {.raw_value = basic_motion.accel_y, .properties = properties};
  475. status.accel.z = {.raw_value = basic_motion.accel_z, .properties = properties};
  476. status.gyro.x = {.raw_value = basic_motion.gyro_x, .properties = properties};
  477. status.gyro.y = {.raw_value = basic_motion.gyro_y, .properties = properties};
  478. status.gyro.z = {.raw_value = basic_motion.gyro_z, .properties = properties};
  479. status.delta_timestamp = basic_motion.delta_timestamp;
  480. return status;
  481. }
  482. void OnChange() {
  483. const Common::Input::CallbackStatus status{
  484. .type = Common::Input::InputType::Motion,
  485. .motion_status = GetStatus(),
  486. };
  487. TriggerOnChange(status);
  488. }
  489. private:
  490. const PadIdentifier identifier;
  491. const int motion_sensor;
  492. const float gyro_threshold;
  493. int callback_key;
  494. InputEngine* input_engine;
  495. };
  496. class InputFromAxisMotion final : public Common::Input::InputDevice {
  497. public:
  498. explicit InputFromAxisMotion(PadIdentifier identifier_, int axis_x_, int axis_y_, int axis_z_,
  499. Common::Input::AnalogProperties properties_x_,
  500. Common::Input::AnalogProperties properties_y_,
  501. Common::Input::AnalogProperties properties_z_,
  502. InputEngine* input_engine_)
  503. : identifier(identifier_), axis_x(axis_x_), axis_y(axis_y_), axis_z(axis_z_),
  504. properties_x(properties_x_), properties_y(properties_y_), properties_z(properties_z_),
  505. input_engine(input_engine_) {
  506. UpdateCallback engine_callback{[this]() { OnChange(); }};
  507. const InputIdentifier x_input_identifier{
  508. .identifier = identifier,
  509. .type = EngineInputType::Analog,
  510. .index = axis_x,
  511. .callback = engine_callback,
  512. };
  513. const InputIdentifier y_input_identifier{
  514. .identifier = identifier,
  515. .type = EngineInputType::Analog,
  516. .index = axis_y,
  517. .callback = engine_callback,
  518. };
  519. const InputIdentifier z_input_identifier{
  520. .identifier = identifier,
  521. .type = EngineInputType::Analog,
  522. .index = axis_z,
  523. .callback = engine_callback,
  524. };
  525. last_axis_x_value = 0.0f;
  526. last_axis_y_value = 0.0f;
  527. last_axis_z_value = 0.0f;
  528. callback_key_x = input_engine->SetCallback(x_input_identifier);
  529. callback_key_y = input_engine->SetCallback(y_input_identifier);
  530. callback_key_z = input_engine->SetCallback(z_input_identifier);
  531. }
  532. ~InputFromAxisMotion() override {
  533. input_engine->DeleteCallback(callback_key_x);
  534. input_engine->DeleteCallback(callback_key_y);
  535. input_engine->DeleteCallback(callback_key_z);
  536. }
  537. Common::Input::MotionStatus GetStatus() const {
  538. Common::Input::MotionStatus status{};
  539. status.gyro.x = {
  540. .raw_value = input_engine->GetAxis(identifier, axis_x),
  541. .properties = properties_x,
  542. };
  543. status.gyro.y = {
  544. .raw_value = input_engine->GetAxis(identifier, axis_y),
  545. .properties = properties_y,
  546. };
  547. status.gyro.z = {
  548. .raw_value = input_engine->GetAxis(identifier, axis_z),
  549. .properties = properties_z,
  550. };
  551. status.delta_timestamp = 5000;
  552. status.force_update = true;
  553. return status;
  554. }
  555. void ForceUpdate() override {
  556. const Common::Input::CallbackStatus status{
  557. .type = Common::Input::InputType::Motion,
  558. .motion_status = GetStatus(),
  559. };
  560. last_axis_x_value = status.motion_status.gyro.x.raw_value;
  561. last_axis_y_value = status.motion_status.gyro.y.raw_value;
  562. last_axis_z_value = status.motion_status.gyro.z.raw_value;
  563. TriggerOnChange(status);
  564. }
  565. void OnChange() {
  566. const Common::Input::CallbackStatus status{
  567. .type = Common::Input::InputType::Motion,
  568. .motion_status = GetStatus(),
  569. };
  570. if (status.motion_status.gyro.x.raw_value != last_axis_x_value ||
  571. status.motion_status.gyro.y.raw_value != last_axis_y_value ||
  572. status.motion_status.gyro.z.raw_value != last_axis_z_value) {
  573. last_axis_x_value = status.motion_status.gyro.x.raw_value;
  574. last_axis_y_value = status.motion_status.gyro.y.raw_value;
  575. last_axis_z_value = status.motion_status.gyro.z.raw_value;
  576. TriggerOnChange(status);
  577. }
  578. }
  579. private:
  580. const PadIdentifier identifier;
  581. const int axis_x;
  582. const int axis_y;
  583. const int axis_z;
  584. const Common::Input::AnalogProperties properties_x;
  585. const Common::Input::AnalogProperties properties_y;
  586. const Common::Input::AnalogProperties properties_z;
  587. int callback_key_x;
  588. int callback_key_y;
  589. int callback_key_z;
  590. float last_axis_x_value;
  591. float last_axis_y_value;
  592. float last_axis_z_value;
  593. InputEngine* input_engine;
  594. };
  595. class InputFromCamera final : public Common::Input::InputDevice {
  596. public:
  597. explicit InputFromCamera(PadIdentifier identifier_, InputEngine* input_engine_)
  598. : identifier(identifier_), input_engine(input_engine_) {
  599. UpdateCallback engine_callback{[this]() { OnChange(); }};
  600. const InputIdentifier input_identifier{
  601. .identifier = identifier,
  602. .type = EngineInputType::Camera,
  603. .index = 0,
  604. .callback = engine_callback,
  605. };
  606. callback_key = input_engine->SetCallback(input_identifier);
  607. }
  608. ~InputFromCamera() override {
  609. input_engine->DeleteCallback(callback_key);
  610. }
  611. Common::Input::CameraStatus GetStatus() const {
  612. return input_engine->GetCamera(identifier);
  613. }
  614. void ForceUpdate() override {
  615. OnChange();
  616. }
  617. void OnChange() {
  618. const auto camera_status = GetStatus();
  619. const Common::Input::CallbackStatus status{
  620. .type = Common::Input::InputType::IrSensor,
  621. .camera_status = camera_status.format,
  622. .raw_data = camera_status.data,
  623. };
  624. TriggerOnChange(status);
  625. }
  626. private:
  627. const PadIdentifier identifier;
  628. int callback_key;
  629. InputEngine* input_engine;
  630. };
  631. class InputFromNfc final : public Common::Input::InputDevice {
  632. public:
  633. explicit InputFromNfc(PadIdentifier identifier_, InputEngine* input_engine_)
  634. : identifier(identifier_), input_engine(input_engine_) {
  635. UpdateCallback engine_callback{[this]() { OnChange(); }};
  636. const InputIdentifier input_identifier{
  637. .identifier = identifier,
  638. .type = EngineInputType::Nfc,
  639. .index = 0,
  640. .callback = engine_callback,
  641. };
  642. callback_key = input_engine->SetCallback(input_identifier);
  643. }
  644. ~InputFromNfc() override {
  645. input_engine->DeleteCallback(callback_key);
  646. }
  647. Common::Input::NfcStatus GetStatus() const {
  648. return input_engine->GetNfc(identifier);
  649. }
  650. void ForceUpdate() override {
  651. OnChange();
  652. }
  653. void OnChange() {
  654. const auto nfc_status = GetStatus();
  655. const Common::Input::CallbackStatus status{
  656. .type = Common::Input::InputType::Nfc,
  657. .nfc_status = nfc_status.state,
  658. .raw_data = nfc_status.data,
  659. };
  660. TriggerOnChange(status);
  661. }
  662. private:
  663. const PadIdentifier identifier;
  664. int callback_key;
  665. InputEngine* input_engine;
  666. };
  667. class OutputFromIdentifier final : public Common::Input::OutputDevice {
  668. public:
  669. explicit OutputFromIdentifier(PadIdentifier identifier_, InputEngine* input_engine_)
  670. : identifier(identifier_), input_engine(input_engine_) {}
  671. void SetLED(const Common::Input::LedStatus& led_status) override {
  672. input_engine->SetLeds(identifier, led_status);
  673. }
  674. Common::Input::VibrationError SetVibration(
  675. const Common::Input::VibrationStatus& vibration_status) override {
  676. return input_engine->SetVibration(identifier, vibration_status);
  677. }
  678. bool IsVibrationEnabled() override {
  679. return input_engine->IsVibrationEnabled(identifier);
  680. }
  681. Common::Input::PollingError SetPollingMode(Common::Input::PollingMode polling_mode) override {
  682. return input_engine->SetPollingMode(identifier, polling_mode);
  683. }
  684. Common::Input::CameraError SetCameraFormat(Common::Input::CameraFormat camera_format) override {
  685. return input_engine->SetCameraFormat(identifier, camera_format);
  686. }
  687. Common::Input::NfcState SupportsNfc() const override {
  688. return input_engine->SupportsNfc(identifier);
  689. }
  690. Common::Input::NfcState WriteNfcData(const std::vector<u8>& data) override {
  691. return input_engine->WriteNfcData(identifier, data);
  692. }
  693. private:
  694. const PadIdentifier identifier;
  695. InputEngine* input_engine;
  696. };
  697. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateButtonDevice(
  698. const Common::ParamPackage& params) {
  699. const PadIdentifier identifier = {
  700. .guid = Common::UUID{params.Get("guid", "")},
  701. .port = static_cast<std::size_t>(params.Get("port", 0)),
  702. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  703. };
  704. const auto button_id = params.Get("button", 0);
  705. const auto keyboard_key = params.Get("code", 0);
  706. const auto toggle = params.Get("toggle", false) != 0;
  707. const auto inverted = params.Get("inverted", false) != 0;
  708. input_engine->PreSetController(identifier);
  709. input_engine->PreSetButton(identifier, button_id);
  710. input_engine->PreSetButton(identifier, keyboard_key);
  711. if (keyboard_key != 0) {
  712. return std::make_unique<InputFromButton>(identifier, keyboard_key, toggle, inverted,
  713. input_engine.get());
  714. }
  715. return std::make_unique<InputFromButton>(identifier, button_id, toggle, inverted,
  716. input_engine.get());
  717. }
  718. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateHatButtonDevice(
  719. const Common::ParamPackage& params) {
  720. const PadIdentifier identifier = {
  721. .guid = Common::UUID{params.Get("guid", "")},
  722. .port = static_cast<std::size_t>(params.Get("port", 0)),
  723. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  724. };
  725. const auto button_id = params.Get("hat", 0);
  726. const auto direction = input_engine->GetHatButtonId(params.Get("direction", ""));
  727. const auto toggle = params.Get("toggle", false) != 0;
  728. const auto inverted = params.Get("inverted", false) != 0;
  729. input_engine->PreSetController(identifier);
  730. input_engine->PreSetHatButton(identifier, button_id);
  731. return std::make_unique<InputFromHatButton>(identifier, button_id, direction, toggle, inverted,
  732. input_engine.get());
  733. }
  734. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateStickDevice(
  735. const Common::ParamPackage& params) {
  736. const auto deadzone = std::clamp(params.Get("deadzone", 0.15f), 0.0f, 1.0f);
  737. const auto range = std::clamp(params.Get("range", 0.95f), 0.25f, 1.50f);
  738. const auto threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f);
  739. const PadIdentifier identifier = {
  740. .guid = Common::UUID{params.Get("guid", "")},
  741. .port = static_cast<std::size_t>(params.Get("port", 0)),
  742. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  743. };
  744. const auto axis_x = params.Get("axis_x", 0);
  745. const Common::Input::AnalogProperties properties_x = {
  746. .deadzone = deadzone,
  747. .range = range,
  748. .threshold = threshold,
  749. .offset = std::clamp(params.Get("offset_x", 0.0f), -1.0f, 1.0f),
  750. .inverted = params.Get("invert_x", "+") == "-",
  751. };
  752. const auto axis_y = params.Get("axis_y", 1);
  753. const Common::Input::AnalogProperties properties_y = {
  754. .deadzone = deadzone,
  755. .range = range,
  756. .threshold = threshold,
  757. .offset = std::clamp(params.Get("offset_y", 0.0f), -1.0f, 1.0f),
  758. .inverted = params.Get("invert_y", "+") != "+",
  759. };
  760. input_engine->PreSetController(identifier);
  761. input_engine->PreSetAxis(identifier, axis_x);
  762. input_engine->PreSetAxis(identifier, axis_y);
  763. return std::make_unique<InputFromStick>(identifier, axis_x, axis_y, properties_x, properties_y,
  764. input_engine.get());
  765. }
  766. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateAnalogDevice(
  767. const Common::ParamPackage& params) {
  768. const PadIdentifier identifier = {
  769. .guid = Common::UUID{params.Get("guid", "")},
  770. .port = static_cast<std::size_t>(params.Get("port", 0)),
  771. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  772. };
  773. const auto axis = params.Get("axis", 0);
  774. const Common::Input::AnalogProperties properties = {
  775. .deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f),
  776. .range = std::clamp(params.Get("range", 1.0f), 0.25f, 1.50f),
  777. .threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f),
  778. .offset = std::clamp(params.Get("offset", 0.0f), -1.0f, 1.0f),
  779. .inverted = params.Get("invert", "+") == "-",
  780. .toggle = params.Get("toggle", false) != 0,
  781. };
  782. input_engine->PreSetController(identifier);
  783. input_engine->PreSetAxis(identifier, axis);
  784. return std::make_unique<InputFromAnalog>(identifier, axis, properties, input_engine.get());
  785. }
  786. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateTriggerDevice(
  787. const Common::ParamPackage& params) {
  788. const PadIdentifier identifier = {
  789. .guid = Common::UUID{params.Get("guid", "")},
  790. .port = static_cast<std::size_t>(params.Get("port", 0)),
  791. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  792. };
  793. const auto button = params.Get("button", 0);
  794. const auto toggle = params.Get("toggle", false) != 0;
  795. const auto inverted = params.Get("inverted", false) != 0;
  796. const auto axis = params.Get("axis", 0);
  797. const Common::Input::AnalogProperties properties = {
  798. .deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f),
  799. .range = std::clamp(params.Get("range", 1.0f), 0.25f, 2.50f),
  800. .threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f),
  801. .offset = std::clamp(params.Get("offset", 0.0f), -1.0f, 1.0f),
  802. .inverted = params.Get("invert", false) != 0,
  803. };
  804. input_engine->PreSetController(identifier);
  805. input_engine->PreSetAxis(identifier, axis);
  806. input_engine->PreSetButton(identifier, button);
  807. return std::make_unique<InputFromTrigger>(identifier, button, toggle, inverted, axis,
  808. properties, input_engine.get());
  809. }
  810. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateTouchDevice(
  811. const Common::ParamPackage& params) {
  812. const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f);
  813. const auto range = std::clamp(params.Get("range", 1.0f), 0.25f, 1.50f);
  814. const auto threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f);
  815. const PadIdentifier identifier = {
  816. .guid = Common::UUID{params.Get("guid", "")},
  817. .port = static_cast<std::size_t>(params.Get("port", 0)),
  818. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  819. };
  820. const auto button = params.Get("button", 0);
  821. const auto toggle = params.Get("toggle", false) != 0;
  822. const auto inverted = params.Get("inverted", false) != 0;
  823. const auto axis_x = params.Get("axis_x", 0);
  824. const Common::Input::AnalogProperties properties_x = {
  825. .deadzone = deadzone,
  826. .range = range,
  827. .threshold = threshold,
  828. .offset = std::clamp(params.Get("offset_x", 0.0f), -1.0f, 1.0f),
  829. .inverted = params.Get("invert_x", "+") == "-",
  830. };
  831. const auto axis_y = params.Get("axis_y", 1);
  832. const Common::Input::AnalogProperties properties_y = {
  833. .deadzone = deadzone,
  834. .range = range,
  835. .threshold = threshold,
  836. .offset = std::clamp(params.Get("offset_y", 0.0f), -1.0f, 1.0f),
  837. .inverted = params.Get("invert_y", false) != 0,
  838. };
  839. input_engine->PreSetController(identifier);
  840. input_engine->PreSetAxis(identifier, axis_x);
  841. input_engine->PreSetAxis(identifier, axis_y);
  842. input_engine->PreSetButton(identifier, button);
  843. return std::make_unique<InputFromTouch>(identifier, button, toggle, inverted, axis_x, axis_y,
  844. properties_x, properties_y, input_engine.get());
  845. }
  846. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateBatteryDevice(
  847. const Common::ParamPackage& params) {
  848. const PadIdentifier identifier = {
  849. .guid = Common::UUID{params.Get("guid", "")},
  850. .port = static_cast<std::size_t>(params.Get("port", 0)),
  851. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  852. };
  853. input_engine->PreSetController(identifier);
  854. return std::make_unique<InputFromBattery>(identifier, input_engine.get());
  855. }
  856. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateMotionDevice(
  857. Common::ParamPackage params) {
  858. const PadIdentifier identifier = {
  859. .guid = Common::UUID{params.Get("guid", "")},
  860. .port = static_cast<std::size_t>(params.Get("port", 0)),
  861. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  862. };
  863. if (params.Has("motion")) {
  864. const auto motion_sensor = params.Get("motion", 0);
  865. const auto gyro_threshold = params.Get("threshold", 0.007f);
  866. input_engine->PreSetController(identifier);
  867. input_engine->PreSetMotion(identifier, motion_sensor);
  868. return std::make_unique<InputFromMotion>(identifier, motion_sensor, gyro_threshold,
  869. input_engine.get());
  870. }
  871. const auto deadzone = std::clamp(params.Get("deadzone", 0.15f), 0.0f, 1.0f);
  872. const auto range = std::clamp(params.Get("range", 1.0f), 0.25f, 1.50f);
  873. const auto threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f);
  874. const auto axis_x = params.Get("axis_x", 0);
  875. const Common::Input::AnalogProperties properties_x = {
  876. .deadzone = deadzone,
  877. .range = range,
  878. .threshold = threshold,
  879. .offset = std::clamp(params.Get("offset_x", 0.0f), -1.0f, 1.0f),
  880. .inverted = params.Get("invert_x", "+") == "-",
  881. };
  882. const auto axis_y = params.Get("axis_y", 1);
  883. const Common::Input::AnalogProperties properties_y = {
  884. .deadzone = deadzone,
  885. .range = range,
  886. .threshold = threshold,
  887. .offset = std::clamp(params.Get("offset_y", 0.0f), -1.0f, 1.0f),
  888. .inverted = params.Get("invert_y", "+") != "+",
  889. };
  890. const auto axis_z = params.Get("axis_z", 1);
  891. const Common::Input::AnalogProperties properties_z = {
  892. .deadzone = deadzone,
  893. .range = range,
  894. .threshold = threshold,
  895. .offset = std::clamp(params.Get("offset_z", 0.0f), -1.0f, 1.0f),
  896. .inverted = params.Get("invert_z", "+") != "+",
  897. };
  898. input_engine->PreSetController(identifier);
  899. input_engine->PreSetAxis(identifier, axis_x);
  900. input_engine->PreSetAxis(identifier, axis_y);
  901. input_engine->PreSetAxis(identifier, axis_z);
  902. return std::make_unique<InputFromAxisMotion>(identifier, axis_x, axis_y, axis_z, properties_x,
  903. properties_y, properties_z, input_engine.get());
  904. }
  905. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateCameraDevice(
  906. const Common::ParamPackage& params) {
  907. const PadIdentifier identifier = {
  908. .guid = Common::UUID{params.Get("guid", "")},
  909. .port = static_cast<std::size_t>(params.Get("port", 0)),
  910. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  911. };
  912. input_engine->PreSetController(identifier);
  913. return std::make_unique<InputFromCamera>(identifier, input_engine.get());
  914. }
  915. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateNfcDevice(
  916. const Common::ParamPackage& params) {
  917. const PadIdentifier identifier = {
  918. .guid = Common::UUID{params.Get("guid", "")},
  919. .port = static_cast<std::size_t>(params.Get("port", 0)),
  920. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  921. };
  922. input_engine->PreSetController(identifier);
  923. return std::make_unique<InputFromNfc>(identifier, input_engine.get());
  924. }
  925. InputFactory::InputFactory(std::shared_ptr<InputEngine> input_engine_)
  926. : input_engine(std::move(input_engine_)) {}
  927. std::unique_ptr<Common::Input::InputDevice> InputFactory::Create(
  928. const Common::ParamPackage& params) {
  929. if (params.Has("battery")) {
  930. return CreateBatteryDevice(params);
  931. }
  932. if (params.Has("camera")) {
  933. return CreateCameraDevice(params);
  934. }
  935. if (params.Has("nfc")) {
  936. return CreateNfcDevice(params);
  937. }
  938. if (params.Has("button") && params.Has("axis")) {
  939. return CreateTriggerDevice(params);
  940. }
  941. if (params.Has("button") && params.Has("axis_x") && params.Has("axis_y")) {
  942. return CreateTouchDevice(params);
  943. }
  944. if (params.Has("button") || params.Has("code")) {
  945. return CreateButtonDevice(params);
  946. }
  947. if (params.Has("hat")) {
  948. return CreateHatButtonDevice(params);
  949. }
  950. if (params.Has("axis_x") && params.Has("axis_y") && params.Has("axis_z")) {
  951. return CreateMotionDevice(params);
  952. }
  953. if (params.Has("motion")) {
  954. return CreateMotionDevice(params);
  955. }
  956. if (params.Has("axis_x") && params.Has("axis_y")) {
  957. return CreateStickDevice(params);
  958. }
  959. if (params.Has("axis")) {
  960. return CreateAnalogDevice(params);
  961. }
  962. LOG_ERROR(Input, "Invalid parameters given");
  963. return std::make_unique<DummyInput>();
  964. }
  965. OutputFactory::OutputFactory(std::shared_ptr<InputEngine> input_engine_)
  966. : input_engine(std::move(input_engine_)) {}
  967. std::unique_ptr<Common::Input::OutputDevice> OutputFactory::Create(
  968. const Common::ParamPackage& params) {
  969. const PadIdentifier identifier = {
  970. .guid = Common::UUID{params.Get("guid", "")},
  971. .port = static_cast<std::size_t>(params.Get("port", 0)),
  972. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  973. };
  974. input_engine->PreSetController(identifier);
  975. return std::make_unique<OutputFromIdentifier>(identifier, input_engine.get());
  976. }
  977. } // namespace InputCommon