input_poller.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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 touch_id_, int button_, bool toggle_,
  204. bool inverted_, int axis_x_, int axis_y_,
  205. Common::Input::AnalogProperties properties_x_,
  206. Common::Input::AnalogProperties properties_y_,
  207. InputEngine* input_engine_)
  208. : identifier(identifier_), touch_id(touch_id_), button(button_), toggle(toggle_),
  209. inverted(inverted_), axis_x(axis_x_), axis_y(axis_y_), properties_x(properties_x_),
  210. properties_y(properties_y_), input_engine(input_engine_) {
  211. UpdateCallback engine_callback{[this]() { OnChange(); }};
  212. const InputIdentifier button_input_identifier{
  213. .identifier = identifier,
  214. .type = EngineInputType::Button,
  215. .index = button,
  216. .callback = engine_callback,
  217. };
  218. const InputIdentifier x_input_identifier{
  219. .identifier = identifier,
  220. .type = EngineInputType::Analog,
  221. .index = axis_x,
  222. .callback = engine_callback,
  223. };
  224. const InputIdentifier y_input_identifier{
  225. .identifier = identifier,
  226. .type = EngineInputType::Analog,
  227. .index = axis_y,
  228. .callback = engine_callback,
  229. };
  230. last_axis_x_value = 0.0f;
  231. last_axis_y_value = 0.0f;
  232. last_button_value = false;
  233. callback_key_button = input_engine->SetCallback(button_input_identifier);
  234. callback_key_x = input_engine->SetCallback(x_input_identifier);
  235. callback_key_y = input_engine->SetCallback(y_input_identifier);
  236. }
  237. ~InputFromTouch() override {
  238. input_engine->DeleteCallback(callback_key_button);
  239. input_engine->DeleteCallback(callback_key_x);
  240. input_engine->DeleteCallback(callback_key_y);
  241. }
  242. Common::Input::TouchStatus GetStatus() const {
  243. Common::Input::TouchStatus status;
  244. status.id = touch_id;
  245. status.pressed = {
  246. .value = input_engine->GetButton(identifier, button),
  247. .inverted = inverted,
  248. .toggle = toggle,
  249. };
  250. status.x = {
  251. .raw_value = input_engine->GetAxis(identifier, axis_x),
  252. .properties = properties_x,
  253. };
  254. status.y = {
  255. .raw_value = input_engine->GetAxis(identifier, axis_y),
  256. .properties = properties_y,
  257. };
  258. return status;
  259. }
  260. void OnChange() {
  261. const Common::Input::CallbackStatus status{
  262. .type = Common::Input::InputType::Touch,
  263. .touch_status = GetStatus(),
  264. };
  265. if (status.touch_status.x.raw_value != last_axis_x_value ||
  266. status.touch_status.y.raw_value != last_axis_y_value ||
  267. status.touch_status.pressed.value != last_button_value) {
  268. last_axis_x_value = status.touch_status.x.raw_value;
  269. last_axis_y_value = status.touch_status.y.raw_value;
  270. last_button_value = status.touch_status.pressed.value;
  271. TriggerOnChange(status);
  272. }
  273. }
  274. private:
  275. const PadIdentifier identifier;
  276. const int touch_id;
  277. const int button;
  278. const bool toggle;
  279. const bool inverted;
  280. const int axis_x;
  281. const int axis_y;
  282. const Common::Input::AnalogProperties properties_x;
  283. const Common::Input::AnalogProperties properties_y;
  284. int callback_key_button;
  285. int callback_key_x;
  286. int callback_key_y;
  287. bool last_button_value;
  288. float last_axis_x_value;
  289. float last_axis_y_value;
  290. InputEngine* input_engine;
  291. };
  292. class InputFromTrigger final : public Common::Input::InputDevice {
  293. public:
  294. explicit InputFromTrigger(PadIdentifier identifier_, int button_, bool toggle_, bool inverted_,
  295. int axis_, Common::Input::AnalogProperties properties_,
  296. InputEngine* input_engine_)
  297. : identifier(identifier_), button(button_), toggle(toggle_), inverted(inverted_),
  298. axis(axis_), properties(properties_), input_engine(input_engine_) {
  299. UpdateCallback engine_callback{[this]() { OnChange(); }};
  300. const InputIdentifier button_input_identifier{
  301. .identifier = identifier,
  302. .type = EngineInputType::Button,
  303. .index = button,
  304. .callback = engine_callback,
  305. };
  306. const InputIdentifier axis_input_identifier{
  307. .identifier = identifier,
  308. .type = EngineInputType::Analog,
  309. .index = axis,
  310. .callback = engine_callback,
  311. };
  312. last_axis_value = 0.0f;
  313. last_button_value = false;
  314. callback_key_button = input_engine->SetCallback(button_input_identifier);
  315. axis_callback_key = input_engine->SetCallback(axis_input_identifier);
  316. }
  317. ~InputFromTrigger() override {
  318. input_engine->DeleteCallback(callback_key_button);
  319. input_engine->DeleteCallback(axis_callback_key);
  320. }
  321. Common::Input::TriggerStatus GetStatus() const {
  322. const Common::Input::AnalogStatus analog_status{
  323. .raw_value = input_engine->GetAxis(identifier, axis),
  324. .properties = properties,
  325. };
  326. const Common::Input::ButtonStatus button_status{
  327. .value = input_engine->GetButton(identifier, button),
  328. .inverted = inverted,
  329. .toggle = toggle,
  330. };
  331. return {
  332. .analog = analog_status,
  333. .pressed = button_status,
  334. };
  335. }
  336. void OnChange() {
  337. const Common::Input::CallbackStatus status{
  338. .type = Common::Input::InputType::Trigger,
  339. .trigger_status = GetStatus(),
  340. };
  341. if (status.trigger_status.analog.raw_value != last_axis_value ||
  342. status.trigger_status.pressed.value != last_button_value) {
  343. last_axis_value = status.trigger_status.analog.raw_value;
  344. last_button_value = status.trigger_status.pressed.value;
  345. TriggerOnChange(status);
  346. }
  347. }
  348. private:
  349. const PadIdentifier identifier;
  350. const int button;
  351. const bool toggle;
  352. const bool inverted;
  353. const int axis;
  354. const Common::Input::AnalogProperties properties;
  355. int callback_key_button;
  356. int axis_callback_key;
  357. bool last_button_value;
  358. float last_axis_value;
  359. InputEngine* input_engine;
  360. };
  361. class InputFromAnalog final : public Common::Input::InputDevice {
  362. public:
  363. explicit InputFromAnalog(PadIdentifier identifier_, int axis_,
  364. Common::Input::AnalogProperties properties_,
  365. InputEngine* input_engine_)
  366. : identifier(identifier_), axis(axis_), properties(properties_),
  367. input_engine(input_engine_) {
  368. UpdateCallback engine_callback{[this]() { OnChange(); }};
  369. const InputIdentifier input_identifier{
  370. .identifier = identifier,
  371. .type = EngineInputType::Analog,
  372. .index = axis,
  373. .callback = engine_callback,
  374. };
  375. last_axis_value = 0.0f;
  376. callback_key = input_engine->SetCallback(input_identifier);
  377. }
  378. ~InputFromAnalog() override {
  379. input_engine->DeleteCallback(callback_key);
  380. }
  381. Common::Input::AnalogStatus GetStatus() const {
  382. return {
  383. .raw_value = input_engine->GetAxis(identifier, axis),
  384. .properties = properties,
  385. };
  386. }
  387. void OnChange() {
  388. const Common::Input::CallbackStatus status{
  389. .type = Common::Input::InputType::Analog,
  390. .analog_status = GetStatus(),
  391. };
  392. if (status.analog_status.raw_value != last_axis_value) {
  393. last_axis_value = status.analog_status.raw_value;
  394. TriggerOnChange(status);
  395. }
  396. }
  397. private:
  398. const PadIdentifier identifier;
  399. const int axis;
  400. const Common::Input::AnalogProperties properties;
  401. int callback_key;
  402. float last_axis_value;
  403. InputEngine* input_engine;
  404. };
  405. class InputFromBattery final : public Common::Input::InputDevice {
  406. public:
  407. explicit InputFromBattery(PadIdentifier identifier_, InputEngine* input_engine_)
  408. : identifier(identifier_), input_engine(input_engine_) {
  409. UpdateCallback engine_callback{[this]() { OnChange(); }};
  410. const InputIdentifier input_identifier{
  411. .identifier = identifier,
  412. .type = EngineInputType::Battery,
  413. .index = 0,
  414. .callback = engine_callback,
  415. };
  416. last_battery_value = Common::Input::BatteryStatus::Charging;
  417. callback_key = input_engine->SetCallback(input_identifier);
  418. }
  419. ~InputFromBattery() override {
  420. input_engine->DeleteCallback(callback_key);
  421. }
  422. Common::Input::BatteryStatus GetStatus() const {
  423. return input_engine->GetBattery(identifier);
  424. }
  425. void ForceUpdate() override {
  426. const Common::Input::CallbackStatus status{
  427. .type = Common::Input::InputType::Battery,
  428. .battery_status = GetStatus(),
  429. };
  430. last_battery_value = status.battery_status;
  431. TriggerOnChange(status);
  432. }
  433. void OnChange() {
  434. const Common::Input::CallbackStatus status{
  435. .type = Common::Input::InputType::Battery,
  436. .battery_status = GetStatus(),
  437. };
  438. if (status.battery_status != last_battery_value) {
  439. last_battery_value = status.battery_status;
  440. TriggerOnChange(status);
  441. }
  442. }
  443. private:
  444. const PadIdentifier identifier;
  445. int callback_key;
  446. Common::Input::BatteryStatus last_battery_value;
  447. InputEngine* input_engine;
  448. };
  449. class InputFromMotion final : public Common::Input::InputDevice {
  450. public:
  451. explicit InputFromMotion(PadIdentifier identifier_, int motion_sensor_, float gyro_threshold_,
  452. InputEngine* input_engine_)
  453. : identifier(identifier_), motion_sensor(motion_sensor_), gyro_threshold(gyro_threshold_),
  454. input_engine(input_engine_) {
  455. UpdateCallback engine_callback{[this]() { OnChange(); }};
  456. const InputIdentifier input_identifier{
  457. .identifier = identifier,
  458. .type = EngineInputType::Motion,
  459. .index = motion_sensor,
  460. .callback = engine_callback,
  461. };
  462. callback_key = input_engine->SetCallback(input_identifier);
  463. }
  464. ~InputFromMotion() override {
  465. input_engine->DeleteCallback(callback_key);
  466. }
  467. Common::Input::MotionStatus GetStatus() const {
  468. const auto basic_motion = input_engine->GetMotion(identifier, motion_sensor);
  469. Common::Input::MotionStatus status{};
  470. const Common::Input::AnalogProperties properties = {
  471. .deadzone = 0.0f,
  472. .range = 1.0f,
  473. .threshold = gyro_threshold,
  474. .offset = 0.0f,
  475. };
  476. status.accel.x = {.raw_value = basic_motion.accel_x, .properties = properties};
  477. status.accel.y = {.raw_value = basic_motion.accel_y, .properties = properties};
  478. status.accel.z = {.raw_value = basic_motion.accel_z, .properties = properties};
  479. status.gyro.x = {.raw_value = basic_motion.gyro_x, .properties = properties};
  480. status.gyro.y = {.raw_value = basic_motion.gyro_y, .properties = properties};
  481. status.gyro.z = {.raw_value = basic_motion.gyro_z, .properties = properties};
  482. status.delta_timestamp = basic_motion.delta_timestamp;
  483. return status;
  484. }
  485. void OnChange() {
  486. const Common::Input::CallbackStatus status{
  487. .type = Common::Input::InputType::Motion,
  488. .motion_status = GetStatus(),
  489. };
  490. TriggerOnChange(status);
  491. }
  492. private:
  493. const PadIdentifier identifier;
  494. const int motion_sensor;
  495. const float gyro_threshold;
  496. int callback_key;
  497. InputEngine* input_engine;
  498. };
  499. class InputFromAxisMotion final : public Common::Input::InputDevice {
  500. public:
  501. explicit InputFromAxisMotion(PadIdentifier identifier_, int axis_x_, int axis_y_, int axis_z_,
  502. Common::Input::AnalogProperties properties_x_,
  503. Common::Input::AnalogProperties properties_y_,
  504. Common::Input::AnalogProperties properties_z_,
  505. InputEngine* input_engine_)
  506. : identifier(identifier_), axis_x(axis_x_), axis_y(axis_y_), axis_z(axis_z_),
  507. properties_x(properties_x_), properties_y(properties_y_), properties_z(properties_z_),
  508. input_engine(input_engine_) {
  509. UpdateCallback engine_callback{[this]() { OnChange(); }};
  510. const InputIdentifier x_input_identifier{
  511. .identifier = identifier,
  512. .type = EngineInputType::Analog,
  513. .index = axis_x,
  514. .callback = engine_callback,
  515. };
  516. const InputIdentifier y_input_identifier{
  517. .identifier = identifier,
  518. .type = EngineInputType::Analog,
  519. .index = axis_y,
  520. .callback = engine_callback,
  521. };
  522. const InputIdentifier z_input_identifier{
  523. .identifier = identifier,
  524. .type = EngineInputType::Analog,
  525. .index = axis_z,
  526. .callback = engine_callback,
  527. };
  528. last_axis_x_value = 0.0f;
  529. last_axis_y_value = 0.0f;
  530. last_axis_z_value = 0.0f;
  531. callback_key_x = input_engine->SetCallback(x_input_identifier);
  532. callback_key_y = input_engine->SetCallback(y_input_identifier);
  533. callback_key_z = input_engine->SetCallback(z_input_identifier);
  534. }
  535. ~InputFromAxisMotion() override {
  536. input_engine->DeleteCallback(callback_key_x);
  537. input_engine->DeleteCallback(callback_key_y);
  538. input_engine->DeleteCallback(callback_key_z);
  539. }
  540. Common::Input::MotionStatus GetStatus() const {
  541. Common::Input::MotionStatus status{};
  542. status.gyro.x = {
  543. .raw_value = input_engine->GetAxis(identifier, axis_x),
  544. .properties = properties_x,
  545. };
  546. status.gyro.y = {
  547. .raw_value = input_engine->GetAxis(identifier, axis_y),
  548. .properties = properties_y,
  549. };
  550. status.gyro.z = {
  551. .raw_value = input_engine->GetAxis(identifier, axis_z),
  552. .properties = properties_z,
  553. };
  554. status.delta_timestamp = 5000;
  555. status.force_update = true;
  556. return status;
  557. }
  558. void ForceUpdate() override {
  559. const Common::Input::CallbackStatus status{
  560. .type = Common::Input::InputType::Motion,
  561. .motion_status = GetStatus(),
  562. };
  563. last_axis_x_value = status.motion_status.gyro.x.raw_value;
  564. last_axis_y_value = status.motion_status.gyro.y.raw_value;
  565. last_axis_z_value = status.motion_status.gyro.z.raw_value;
  566. TriggerOnChange(status);
  567. }
  568. void OnChange() {
  569. const Common::Input::CallbackStatus status{
  570. .type = Common::Input::InputType::Motion,
  571. .motion_status = GetStatus(),
  572. };
  573. if (status.motion_status.gyro.x.raw_value != last_axis_x_value ||
  574. status.motion_status.gyro.y.raw_value != last_axis_y_value ||
  575. status.motion_status.gyro.z.raw_value != last_axis_z_value) {
  576. last_axis_x_value = status.motion_status.gyro.x.raw_value;
  577. last_axis_y_value = status.motion_status.gyro.y.raw_value;
  578. last_axis_z_value = status.motion_status.gyro.z.raw_value;
  579. TriggerOnChange(status);
  580. }
  581. }
  582. private:
  583. const PadIdentifier identifier;
  584. const int axis_x;
  585. const int axis_y;
  586. const int axis_z;
  587. const Common::Input::AnalogProperties properties_x;
  588. const Common::Input::AnalogProperties properties_y;
  589. const Common::Input::AnalogProperties properties_z;
  590. int callback_key_x;
  591. int callback_key_y;
  592. int callback_key_z;
  593. float last_axis_x_value;
  594. float last_axis_y_value;
  595. float last_axis_z_value;
  596. InputEngine* input_engine;
  597. };
  598. class InputFromCamera final : public Common::Input::InputDevice {
  599. public:
  600. explicit InputFromCamera(PadIdentifier identifier_, InputEngine* input_engine_)
  601. : identifier(identifier_), input_engine(input_engine_) {
  602. UpdateCallback engine_callback{[this]() { OnChange(); }};
  603. const InputIdentifier input_identifier{
  604. .identifier = identifier,
  605. .type = EngineInputType::Camera,
  606. .index = 0,
  607. .callback = engine_callback,
  608. };
  609. callback_key = input_engine->SetCallback(input_identifier);
  610. }
  611. ~InputFromCamera() override {
  612. input_engine->DeleteCallback(callback_key);
  613. }
  614. Common::Input::CameraStatus GetStatus() const {
  615. return input_engine->GetCamera(identifier);
  616. }
  617. void ForceUpdate() override {
  618. OnChange();
  619. }
  620. void OnChange() {
  621. const Common::Input::CallbackStatus status{
  622. .type = Common::Input::InputType::IrSensor,
  623. .camera_status = GetStatus(),
  624. };
  625. TriggerOnChange(status);
  626. }
  627. private:
  628. const PadIdentifier identifier;
  629. int callback_key;
  630. InputEngine* input_engine;
  631. };
  632. class InputFromNfc final : public Common::Input::InputDevice {
  633. public:
  634. explicit InputFromNfc(PadIdentifier identifier_, InputEngine* input_engine_)
  635. : identifier(identifier_), input_engine(input_engine_) {
  636. UpdateCallback engine_callback{[this]() { OnChange(); }};
  637. const InputIdentifier input_identifier{
  638. .identifier = identifier,
  639. .type = EngineInputType::Nfc,
  640. .index = 0,
  641. .callback = engine_callback,
  642. };
  643. callback_key = input_engine->SetCallback(input_identifier);
  644. }
  645. ~InputFromNfc() override {
  646. input_engine->DeleteCallback(callback_key);
  647. }
  648. Common::Input::NfcStatus GetStatus() const {
  649. return input_engine->GetNfc(identifier);
  650. }
  651. void ForceUpdate() override {
  652. OnChange();
  653. }
  654. void OnChange() {
  655. const Common::Input::CallbackStatus status{
  656. .type = Common::Input::InputType::Nfc,
  657. .nfc_status = GetStatus(),
  658. };
  659. TriggerOnChange(status);
  660. }
  661. private:
  662. const PadIdentifier identifier;
  663. int callback_key;
  664. InputEngine* input_engine;
  665. };
  666. class OutputFromIdentifier final : public Common::Input::OutputDevice {
  667. public:
  668. explicit OutputFromIdentifier(PadIdentifier identifier_, InputEngine* input_engine_)
  669. : identifier(identifier_), input_engine(input_engine_) {}
  670. void SetLED(const Common::Input::LedStatus& led_status) override {
  671. input_engine->SetLeds(identifier, led_status);
  672. }
  673. Common::Input::VibrationError SetVibration(
  674. const Common::Input::VibrationStatus& vibration_status) override {
  675. return input_engine->SetRumble(identifier, vibration_status);
  676. }
  677. Common::Input::PollingError SetPollingMode(Common::Input::PollingMode polling_mode) override {
  678. return input_engine->SetPollingMode(identifier, polling_mode);
  679. }
  680. Common::Input::CameraError SetCameraFormat(Common::Input::CameraFormat camera_format) override {
  681. return input_engine->SetCameraFormat(identifier, camera_format);
  682. }
  683. Common::Input::NfcState SupportsNfc() const override {
  684. return input_engine->SupportsNfc(identifier);
  685. }
  686. Common::Input::NfcState WriteNfcData(const std::vector<u8>& data) override {
  687. return input_engine->WriteNfcData(identifier, data);
  688. }
  689. private:
  690. const PadIdentifier identifier;
  691. InputEngine* input_engine;
  692. };
  693. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateButtonDevice(
  694. const Common::ParamPackage& params) {
  695. const PadIdentifier identifier = {
  696. .guid = Common::UUID{params.Get("guid", "")},
  697. .port = static_cast<std::size_t>(params.Get("port", 0)),
  698. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  699. };
  700. const auto button_id = params.Get("button", 0);
  701. const auto keyboard_key = params.Get("code", 0);
  702. const auto toggle = params.Get("toggle", false);
  703. const auto inverted = params.Get("inverted", false);
  704. input_engine->PreSetController(identifier);
  705. input_engine->PreSetButton(identifier, button_id);
  706. input_engine->PreSetButton(identifier, keyboard_key);
  707. if (keyboard_key != 0) {
  708. return std::make_unique<InputFromButton>(identifier, keyboard_key, toggle, inverted,
  709. input_engine.get());
  710. }
  711. return std::make_unique<InputFromButton>(identifier, button_id, toggle, inverted,
  712. input_engine.get());
  713. }
  714. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateHatButtonDevice(
  715. const Common::ParamPackage& params) {
  716. const PadIdentifier identifier = {
  717. .guid = Common::UUID{params.Get("guid", "")},
  718. .port = static_cast<std::size_t>(params.Get("port", 0)),
  719. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  720. };
  721. const auto button_id = params.Get("hat", 0);
  722. const auto direction = input_engine->GetHatButtonId(params.Get("direction", ""));
  723. const auto toggle = params.Get("toggle", false);
  724. const auto inverted = params.Get("inverted", false);
  725. input_engine->PreSetController(identifier);
  726. input_engine->PreSetHatButton(identifier, button_id);
  727. return std::make_unique<InputFromHatButton>(identifier, button_id, direction, toggle, inverted,
  728. input_engine.get());
  729. }
  730. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateStickDevice(
  731. const Common::ParamPackage& params) {
  732. const auto deadzone = std::clamp(params.Get("deadzone", 0.15f), 0.0f, 1.0f);
  733. const auto range = std::clamp(params.Get("range", 0.95f), 0.25f, 1.50f);
  734. const auto threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f);
  735. const PadIdentifier identifier = {
  736. .guid = Common::UUID{params.Get("guid", "")},
  737. .port = static_cast<std::size_t>(params.Get("port", 0)),
  738. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  739. };
  740. const auto axis_x = params.Get("axis_x", 0);
  741. const Common::Input::AnalogProperties properties_x = {
  742. .deadzone = deadzone,
  743. .range = range,
  744. .threshold = threshold,
  745. .offset = std::clamp(params.Get("offset_x", 0.0f), -1.0f, 1.0f),
  746. .inverted = params.Get("invert_x", "+") == "-",
  747. };
  748. const auto axis_y = params.Get("axis_y", 1);
  749. const Common::Input::AnalogProperties properties_y = {
  750. .deadzone = deadzone,
  751. .range = range,
  752. .threshold = threshold,
  753. .offset = std::clamp(params.Get("offset_y", 0.0f), -1.0f, 1.0f),
  754. .inverted = params.Get("invert_y", "+") != "+",
  755. };
  756. input_engine->PreSetController(identifier);
  757. input_engine->PreSetAxis(identifier, axis_x);
  758. input_engine->PreSetAxis(identifier, axis_y);
  759. return std::make_unique<InputFromStick>(identifier, axis_x, axis_y, properties_x, properties_y,
  760. input_engine.get());
  761. }
  762. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateAnalogDevice(
  763. const Common::ParamPackage& params) {
  764. const PadIdentifier identifier = {
  765. .guid = Common::UUID{params.Get("guid", "")},
  766. .port = static_cast<std::size_t>(params.Get("port", 0)),
  767. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  768. };
  769. const auto axis = params.Get("axis", 0);
  770. const Common::Input::AnalogProperties properties = {
  771. .deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f),
  772. .range = std::clamp(params.Get("range", 1.0f), 0.25f, 1.50f),
  773. .threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f),
  774. .offset = std::clamp(params.Get("offset", 0.0f), -1.0f, 1.0f),
  775. .inverted = params.Get("invert", "+") == "-",
  776. .toggle = static_cast<bool>(params.Get("toggle", false)),
  777. };
  778. input_engine->PreSetController(identifier);
  779. input_engine->PreSetAxis(identifier, axis);
  780. return std::make_unique<InputFromAnalog>(identifier, axis, properties, input_engine.get());
  781. }
  782. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateTriggerDevice(
  783. const Common::ParamPackage& params) {
  784. const PadIdentifier identifier = {
  785. .guid = Common::UUID{params.Get("guid", "")},
  786. .port = static_cast<std::size_t>(params.Get("port", 0)),
  787. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  788. };
  789. const auto button = params.Get("button", 0);
  790. const auto toggle = params.Get("toggle", false);
  791. const auto inverted = params.Get("inverted", false);
  792. const auto axis = params.Get("axis", 0);
  793. const Common::Input::AnalogProperties properties = {
  794. .deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f),
  795. .range = std::clamp(params.Get("range", 1.0f), 0.25f, 2.50f),
  796. .threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f),
  797. .offset = std::clamp(params.Get("offset", 0.0f), -1.0f, 1.0f),
  798. .inverted = params.Get("invert", false) != 0,
  799. };
  800. input_engine->PreSetController(identifier);
  801. input_engine->PreSetAxis(identifier, axis);
  802. input_engine->PreSetButton(identifier, button);
  803. return std::make_unique<InputFromTrigger>(identifier, button, toggle, inverted, axis,
  804. properties, input_engine.get());
  805. }
  806. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateTouchDevice(
  807. const Common::ParamPackage& params) {
  808. const auto touch_id = params.Get("touch_id", 0);
  809. const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f);
  810. const auto range = std::clamp(params.Get("range", 1.0f), 0.25f, 1.50f);
  811. const auto threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f);
  812. const PadIdentifier identifier = {
  813. .guid = Common::UUID{params.Get("guid", "")},
  814. .port = static_cast<std::size_t>(params.Get("port", 0)),
  815. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  816. };
  817. const auto button = params.Get("button", 0);
  818. const auto toggle = params.Get("toggle", false);
  819. const auto inverted = params.Get("inverted", false);
  820. const auto axis_x = params.Get("axis_x", 0);
  821. const Common::Input::AnalogProperties properties_x = {
  822. .deadzone = deadzone,
  823. .range = range,
  824. .threshold = threshold,
  825. .offset = std::clamp(params.Get("offset_x", 0.0f), -1.0f, 1.0f),
  826. .inverted = params.Get("invert_x", "+") == "-",
  827. };
  828. const auto axis_y = params.Get("axis_y", 1);
  829. const Common::Input::AnalogProperties properties_y = {
  830. .deadzone = deadzone,
  831. .range = range,
  832. .threshold = threshold,
  833. .offset = std::clamp(params.Get("offset_y", 0.0f), -1.0f, 1.0f),
  834. .inverted = params.Get("invert_y", false) != 0,
  835. };
  836. input_engine->PreSetController(identifier);
  837. input_engine->PreSetAxis(identifier, axis_x);
  838. input_engine->PreSetAxis(identifier, axis_y);
  839. input_engine->PreSetButton(identifier, button);
  840. return std::make_unique<InputFromTouch>(identifier, touch_id, button, toggle, inverted, axis_x,
  841. axis_y, properties_x, properties_y, input_engine.get());
  842. }
  843. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateBatteryDevice(
  844. const Common::ParamPackage& params) {
  845. const PadIdentifier identifier = {
  846. .guid = Common::UUID{params.Get("guid", "")},
  847. .port = static_cast<std::size_t>(params.Get("port", 0)),
  848. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  849. };
  850. input_engine->PreSetController(identifier);
  851. return std::make_unique<InputFromBattery>(identifier, input_engine.get());
  852. }
  853. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateMotionDevice(
  854. Common::ParamPackage params) {
  855. const PadIdentifier identifier = {
  856. .guid = Common::UUID{params.Get("guid", "")},
  857. .port = static_cast<std::size_t>(params.Get("port", 0)),
  858. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  859. };
  860. if (params.Has("motion")) {
  861. const auto motion_sensor = params.Get("motion", 0);
  862. const auto gyro_threshold = params.Get("threshold", 0.007f);
  863. input_engine->PreSetController(identifier);
  864. input_engine->PreSetMotion(identifier, motion_sensor);
  865. return std::make_unique<InputFromMotion>(identifier, motion_sensor, gyro_threshold,
  866. input_engine.get());
  867. }
  868. const auto deadzone = std::clamp(params.Get("deadzone", 0.15f), 0.0f, 1.0f);
  869. const auto range = std::clamp(params.Get("range", 1.0f), 0.25f, 1.50f);
  870. const auto threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f);
  871. const auto axis_x = params.Get("axis_x", 0);
  872. const Common::Input::AnalogProperties properties_x = {
  873. .deadzone = deadzone,
  874. .range = range,
  875. .threshold = threshold,
  876. .offset = std::clamp(params.Get("offset_x", 0.0f), -1.0f, 1.0f),
  877. .inverted = params.Get("invert_x", "+") == "-",
  878. };
  879. const auto axis_y = params.Get("axis_y", 1);
  880. const Common::Input::AnalogProperties properties_y = {
  881. .deadzone = deadzone,
  882. .range = range,
  883. .threshold = threshold,
  884. .offset = std::clamp(params.Get("offset_y", 0.0f), -1.0f, 1.0f),
  885. .inverted = params.Get("invert_y", "+") != "+",
  886. };
  887. const auto axis_z = params.Get("axis_z", 1);
  888. const Common::Input::AnalogProperties properties_z = {
  889. .deadzone = deadzone,
  890. .range = range,
  891. .threshold = threshold,
  892. .offset = std::clamp(params.Get("offset_z", 0.0f), -1.0f, 1.0f),
  893. .inverted = params.Get("invert_z", "+") != "+",
  894. };
  895. input_engine->PreSetController(identifier);
  896. input_engine->PreSetAxis(identifier, axis_x);
  897. input_engine->PreSetAxis(identifier, axis_y);
  898. input_engine->PreSetAxis(identifier, axis_z);
  899. return std::make_unique<InputFromAxisMotion>(identifier, axis_x, axis_y, axis_z, properties_x,
  900. properties_y, properties_z, input_engine.get());
  901. }
  902. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateCameraDevice(
  903. const Common::ParamPackage& params) {
  904. const PadIdentifier identifier = {
  905. .guid = Common::UUID{params.Get("guid", "")},
  906. .port = static_cast<std::size_t>(params.Get("port", 0)),
  907. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  908. };
  909. input_engine->PreSetController(identifier);
  910. return std::make_unique<InputFromCamera>(identifier, input_engine.get());
  911. }
  912. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateNfcDevice(
  913. const Common::ParamPackage& params) {
  914. const PadIdentifier identifier = {
  915. .guid = Common::UUID{params.Get("guid", "")},
  916. .port = static_cast<std::size_t>(params.Get("port", 0)),
  917. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  918. };
  919. input_engine->PreSetController(identifier);
  920. return std::make_unique<InputFromNfc>(identifier, input_engine.get());
  921. }
  922. InputFactory::InputFactory(std::shared_ptr<InputEngine> input_engine_)
  923. : input_engine(std::move(input_engine_)) {}
  924. std::unique_ptr<Common::Input::InputDevice> InputFactory::Create(
  925. const Common::ParamPackage& params) {
  926. if (params.Has("battery")) {
  927. return CreateBatteryDevice(params);
  928. }
  929. if (params.Has("camera")) {
  930. return CreateCameraDevice(params);
  931. }
  932. if (params.Has("nfc")) {
  933. return CreateNfcDevice(params);
  934. }
  935. if (params.Has("button") && params.Has("axis")) {
  936. return CreateTriggerDevice(params);
  937. }
  938. if (params.Has("button") && params.Has("axis_x") && params.Has("axis_y")) {
  939. return CreateTouchDevice(params);
  940. }
  941. if (params.Has("button") || params.Has("code")) {
  942. return CreateButtonDevice(params);
  943. }
  944. if (params.Has("hat")) {
  945. return CreateHatButtonDevice(params);
  946. }
  947. if (params.Has("axis_x") && params.Has("axis_y") && params.Has("axis_z")) {
  948. return CreateMotionDevice(params);
  949. }
  950. if (params.Has("motion")) {
  951. return CreateMotionDevice(params);
  952. }
  953. if (params.Has("axis_x") && params.Has("axis_y")) {
  954. return CreateStickDevice(params);
  955. }
  956. if (params.Has("axis")) {
  957. return CreateAnalogDevice(params);
  958. }
  959. LOG_ERROR(Input, "Invalid parameters given");
  960. return std::make_unique<DummyInput>();
  961. }
  962. OutputFactory::OutputFactory(std::shared_ptr<InputEngine> input_engine_)
  963. : input_engine(std::move(input_engine_)) {}
  964. std::unique_ptr<Common::Input::OutputDevice> OutputFactory::Create(
  965. const Common::ParamPackage& params) {
  966. const PadIdentifier identifier = {
  967. .guid = Common::UUID{params.Get("guid", "")},
  968. .port = static_cast<std::size_t>(params.Get("port", 0)),
  969. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  970. };
  971. input_engine->PreSetController(identifier);
  972. return std::make_unique<OutputFromIdentifier>(identifier, input_engine.get());
  973. }
  974. } // namespace InputCommon