input_poller.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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 OutputFromIdentifier final : public Common::Input::OutputDevice {
  633. public:
  634. explicit OutputFromIdentifier(PadIdentifier identifier_, InputEngine* input_engine_)
  635. : identifier(identifier_), input_engine(input_engine_) {}
  636. void SetLED(const Common::Input::LedStatus& led_status) override {
  637. input_engine->SetLeds(identifier, led_status);
  638. }
  639. Common::Input::VibrationError SetVibration(
  640. const Common::Input::VibrationStatus& vibration_status) override {
  641. return input_engine->SetRumble(identifier, vibration_status);
  642. }
  643. Common::Input::PollingError SetPollingMode(Common::Input::PollingMode polling_mode) override {
  644. return input_engine->SetPollingMode(identifier, polling_mode);
  645. }
  646. Common::Input::CameraError SetCameraFormat(Common::Input::CameraFormat camera_format) override {
  647. return input_engine->SetCameraFormat(identifier, camera_format);
  648. }
  649. private:
  650. const PadIdentifier identifier;
  651. InputEngine* input_engine;
  652. };
  653. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateButtonDevice(
  654. const Common::ParamPackage& params) {
  655. const PadIdentifier identifier = {
  656. .guid = Common::UUID{params.Get("guid", "")},
  657. .port = static_cast<std::size_t>(params.Get("port", 0)),
  658. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  659. };
  660. const auto button_id = params.Get("button", 0);
  661. const auto keyboard_key = params.Get("code", 0);
  662. const auto toggle = params.Get("toggle", false);
  663. const auto inverted = params.Get("inverted", false);
  664. input_engine->PreSetController(identifier);
  665. input_engine->PreSetButton(identifier, button_id);
  666. input_engine->PreSetButton(identifier, keyboard_key);
  667. if (keyboard_key != 0) {
  668. return std::make_unique<InputFromButton>(identifier, keyboard_key, toggle, inverted,
  669. input_engine.get());
  670. }
  671. return std::make_unique<InputFromButton>(identifier, button_id, toggle, inverted,
  672. input_engine.get());
  673. }
  674. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateHatButtonDevice(
  675. const Common::ParamPackage& params) {
  676. const PadIdentifier identifier = {
  677. .guid = Common::UUID{params.Get("guid", "")},
  678. .port = static_cast<std::size_t>(params.Get("port", 0)),
  679. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  680. };
  681. const auto button_id = params.Get("hat", 0);
  682. const auto direction = input_engine->GetHatButtonId(params.Get("direction", ""));
  683. const auto toggle = params.Get("toggle", false);
  684. const auto inverted = params.Get("inverted", false);
  685. input_engine->PreSetController(identifier);
  686. input_engine->PreSetHatButton(identifier, button_id);
  687. return std::make_unique<InputFromHatButton>(identifier, button_id, direction, toggle, inverted,
  688. input_engine.get());
  689. }
  690. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateStickDevice(
  691. const Common::ParamPackage& params) {
  692. const auto deadzone = std::clamp(params.Get("deadzone", 0.15f), 0.0f, 1.0f);
  693. const auto range = std::clamp(params.Get("range", 0.95f), 0.25f, 1.50f);
  694. const auto threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f);
  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 axis_x = params.Get("axis_x", 0);
  701. const Common::Input::AnalogProperties properties_x = {
  702. .deadzone = deadzone,
  703. .range = range,
  704. .threshold = threshold,
  705. .offset = std::clamp(params.Get("offset_x", 0.0f), -1.0f, 1.0f),
  706. .inverted = params.Get("invert_x", "+") == "-",
  707. };
  708. const auto axis_y = params.Get("axis_y", 1);
  709. const Common::Input::AnalogProperties properties_y = {
  710. .deadzone = deadzone,
  711. .range = range,
  712. .threshold = threshold,
  713. .offset = std::clamp(params.Get("offset_y", 0.0f), -1.0f, 1.0f),
  714. .inverted = params.Get("invert_y", "+") != "+",
  715. };
  716. input_engine->PreSetController(identifier);
  717. input_engine->PreSetAxis(identifier, axis_x);
  718. input_engine->PreSetAxis(identifier, axis_y);
  719. return std::make_unique<InputFromStick>(identifier, axis_x, axis_y, properties_x, properties_y,
  720. input_engine.get());
  721. }
  722. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateAnalogDevice(
  723. const Common::ParamPackage& params) {
  724. const PadIdentifier identifier = {
  725. .guid = Common::UUID{params.Get("guid", "")},
  726. .port = static_cast<std::size_t>(params.Get("port", 0)),
  727. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  728. };
  729. const auto axis = params.Get("axis", 0);
  730. const Common::Input::AnalogProperties properties = {
  731. .deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f),
  732. .range = std::clamp(params.Get("range", 1.0f), 0.25f, 1.50f),
  733. .threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f),
  734. .offset = std::clamp(params.Get("offset", 0.0f), -1.0f, 1.0f),
  735. .inverted = params.Get("invert", "+") == "-",
  736. .toggle = static_cast<bool>(params.Get("toggle", false)),
  737. };
  738. input_engine->PreSetController(identifier);
  739. input_engine->PreSetAxis(identifier, axis);
  740. return std::make_unique<InputFromAnalog>(identifier, axis, properties, input_engine.get());
  741. }
  742. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateTriggerDevice(
  743. const Common::ParamPackage& params) {
  744. const PadIdentifier identifier = {
  745. .guid = Common::UUID{params.Get("guid", "")},
  746. .port = static_cast<std::size_t>(params.Get("port", 0)),
  747. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  748. };
  749. const auto button = params.Get("button", 0);
  750. const auto toggle = params.Get("toggle", false);
  751. const auto inverted = params.Get("inverted", false);
  752. const auto axis = params.Get("axis", 0);
  753. const Common::Input::AnalogProperties properties = {
  754. .deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f),
  755. .range = std::clamp(params.Get("range", 1.0f), 0.25f, 2.50f),
  756. .threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f),
  757. .offset = std::clamp(params.Get("offset", 0.0f), -1.0f, 1.0f),
  758. .inverted = params.Get("invert", false) != 0,
  759. };
  760. input_engine->PreSetController(identifier);
  761. input_engine->PreSetAxis(identifier, axis);
  762. input_engine->PreSetButton(identifier, button);
  763. return std::make_unique<InputFromTrigger>(identifier, button, toggle, inverted, axis,
  764. properties, input_engine.get());
  765. }
  766. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateTouchDevice(
  767. const Common::ParamPackage& params) {
  768. const auto touch_id = params.Get("touch_id", 0);
  769. const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f);
  770. const auto range = std::clamp(params.Get("range", 1.0f), 0.25f, 1.50f);
  771. const auto threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f);
  772. const PadIdentifier identifier = {
  773. .guid = Common::UUID{params.Get("guid", "")},
  774. .port = static_cast<std::size_t>(params.Get("port", 0)),
  775. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  776. };
  777. const auto button = params.Get("button", 0);
  778. const auto toggle = params.Get("toggle", false);
  779. const auto inverted = params.Get("inverted", false);
  780. const auto axis_x = params.Get("axis_x", 0);
  781. const Common::Input::AnalogProperties properties_x = {
  782. .deadzone = deadzone,
  783. .range = range,
  784. .threshold = threshold,
  785. .offset = std::clamp(params.Get("offset_x", 0.0f), -1.0f, 1.0f),
  786. .inverted = params.Get("invert_x", "+") == "-",
  787. };
  788. const auto axis_y = params.Get("axis_y", 1);
  789. const Common::Input::AnalogProperties properties_y = {
  790. .deadzone = deadzone,
  791. .range = range,
  792. .threshold = threshold,
  793. .offset = std::clamp(params.Get("offset_y", 0.0f), -1.0f, 1.0f),
  794. .inverted = params.Get("invert_y", false) != 0,
  795. };
  796. input_engine->PreSetController(identifier);
  797. input_engine->PreSetAxis(identifier, axis_x);
  798. input_engine->PreSetAxis(identifier, axis_y);
  799. input_engine->PreSetButton(identifier, button);
  800. return std::make_unique<InputFromTouch>(identifier, touch_id, button, toggle, inverted, axis_x,
  801. axis_y, properties_x, properties_y, input_engine.get());
  802. }
  803. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateBatteryDevice(
  804. const Common::ParamPackage& params) {
  805. const PadIdentifier identifier = {
  806. .guid = Common::UUID{params.Get("guid", "")},
  807. .port = static_cast<std::size_t>(params.Get("port", 0)),
  808. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  809. };
  810. input_engine->PreSetController(identifier);
  811. return std::make_unique<InputFromBattery>(identifier, input_engine.get());
  812. }
  813. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateMotionDevice(
  814. Common::ParamPackage params) {
  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. if (params.Has("motion")) {
  821. const auto motion_sensor = params.Get("motion", 0);
  822. const auto gyro_threshold = params.Get("threshold", 0.007f);
  823. input_engine->PreSetController(identifier);
  824. input_engine->PreSetMotion(identifier, motion_sensor);
  825. return std::make_unique<InputFromMotion>(identifier, motion_sensor, gyro_threshold,
  826. input_engine.get());
  827. }
  828. const auto deadzone = std::clamp(params.Get("deadzone", 0.15f), 0.0f, 1.0f);
  829. const auto range = std::clamp(params.Get("range", 1.0f), 0.25f, 1.50f);
  830. const auto threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f);
  831. const auto axis_x = params.Get("axis_x", 0);
  832. const Common::Input::AnalogProperties properties_x = {
  833. .deadzone = deadzone,
  834. .range = range,
  835. .threshold = threshold,
  836. .offset = std::clamp(params.Get("offset_x", 0.0f), -1.0f, 1.0f),
  837. .inverted = params.Get("invert_x", "+") == "-",
  838. };
  839. const auto axis_y = params.Get("axis_y", 1);
  840. const Common::Input::AnalogProperties properties_y = {
  841. .deadzone = deadzone,
  842. .range = range,
  843. .threshold = threshold,
  844. .offset = std::clamp(params.Get("offset_y", 0.0f), -1.0f, 1.0f),
  845. .inverted = params.Get("invert_y", "+") != "+",
  846. };
  847. const auto axis_z = params.Get("axis_z", 1);
  848. const Common::Input::AnalogProperties properties_z = {
  849. .deadzone = deadzone,
  850. .range = range,
  851. .threshold = threshold,
  852. .offset = std::clamp(params.Get("offset_z", 0.0f), -1.0f, 1.0f),
  853. .inverted = params.Get("invert_z", "+") != "+",
  854. };
  855. input_engine->PreSetController(identifier);
  856. input_engine->PreSetAxis(identifier, axis_x);
  857. input_engine->PreSetAxis(identifier, axis_y);
  858. input_engine->PreSetAxis(identifier, axis_z);
  859. return std::make_unique<InputFromAxisMotion>(identifier, axis_x, axis_y, axis_z, properties_x,
  860. properties_y, properties_z, input_engine.get());
  861. }
  862. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateCameraDevice(
  863. const Common::ParamPackage& params) {
  864. const PadIdentifier identifier = {
  865. .guid = Common::UUID{params.Get("guid", "")},
  866. .port = static_cast<std::size_t>(params.Get("port", 0)),
  867. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  868. };
  869. input_engine->PreSetController(identifier);
  870. return std::make_unique<InputFromCamera>(identifier, input_engine.get());
  871. }
  872. InputFactory::InputFactory(std::shared_ptr<InputEngine> input_engine_)
  873. : input_engine(std::move(input_engine_)) {}
  874. std::unique_ptr<Common::Input::InputDevice> InputFactory::Create(
  875. const Common::ParamPackage& params) {
  876. if (params.Has("battery")) {
  877. return CreateBatteryDevice(params);
  878. }
  879. if (params.Has("camera")) {
  880. return CreateCameraDevice(params);
  881. }
  882. if (params.Has("button") && params.Has("axis")) {
  883. return CreateTriggerDevice(params);
  884. }
  885. if (params.Has("button") && params.Has("axis_x") && params.Has("axis_y")) {
  886. return CreateTouchDevice(params);
  887. }
  888. if (params.Has("button") || params.Has("code")) {
  889. return CreateButtonDevice(params);
  890. }
  891. if (params.Has("hat")) {
  892. return CreateHatButtonDevice(params);
  893. }
  894. if (params.Has("axis_x") && params.Has("axis_y") && params.Has("axis_z")) {
  895. return CreateMotionDevice(params);
  896. }
  897. if (params.Has("motion")) {
  898. return CreateMotionDevice(params);
  899. }
  900. if (params.Has("axis_x") && params.Has("axis_y")) {
  901. return CreateStickDevice(params);
  902. }
  903. if (params.Has("axis")) {
  904. return CreateAnalogDevice(params);
  905. }
  906. LOG_ERROR(Input, "Invalid parameters given");
  907. return std::make_unique<DummyInput>();
  908. }
  909. OutputFactory::OutputFactory(std::shared_ptr<InputEngine> input_engine_)
  910. : input_engine(std::move(input_engine_)) {}
  911. std::unique_ptr<Common::Input::OutputDevice> OutputFactory::Create(
  912. const Common::ParamPackage& params) {
  913. const PadIdentifier identifier = {
  914. .guid = Common::UUID{params.Get("guid", "")},
  915. .port = static_cast<std::size_t>(params.Get("port", 0)),
  916. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  917. };
  918. input_engine->PreSetController(identifier);
  919. return std::make_unique<OutputFromIdentifier>(identifier, input_engine.get());
  920. }
  921. } // namespace InputCommon