input_poller.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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 auto camera_status = GetStatus();
  622. const Common::Input::CallbackStatus status{
  623. .type = Common::Input::InputType::IrSensor,
  624. .camera_status = camera_status.format,
  625. .raw_data = camera_status.data,
  626. };
  627. TriggerOnChange(status);
  628. }
  629. private:
  630. const PadIdentifier identifier;
  631. int callback_key;
  632. InputEngine* input_engine;
  633. };
  634. class InputFromNfc final : public Common::Input::InputDevice {
  635. public:
  636. explicit InputFromNfc(PadIdentifier identifier_, InputEngine* input_engine_)
  637. : identifier(identifier_), input_engine(input_engine_) {
  638. UpdateCallback engine_callback{[this]() { OnChange(); }};
  639. const InputIdentifier input_identifier{
  640. .identifier = identifier,
  641. .type = EngineInputType::Nfc,
  642. .index = 0,
  643. .callback = engine_callback,
  644. };
  645. callback_key = input_engine->SetCallback(input_identifier);
  646. }
  647. ~InputFromNfc() override {
  648. input_engine->DeleteCallback(callback_key);
  649. }
  650. Common::Input::NfcStatus GetStatus() const {
  651. return input_engine->GetNfc(identifier);
  652. }
  653. void ForceUpdate() override {
  654. OnChange();
  655. }
  656. void OnChange() {
  657. const auto nfc_status = GetStatus();
  658. const Common::Input::CallbackStatus status{
  659. .type = Common::Input::InputType::Nfc,
  660. .nfc_status = nfc_status.state,
  661. .raw_data = nfc_status.data,
  662. };
  663. TriggerOnChange(status);
  664. }
  665. private:
  666. const PadIdentifier identifier;
  667. int callback_key;
  668. InputEngine* input_engine;
  669. };
  670. class OutputFromIdentifier final : public Common::Input::OutputDevice {
  671. public:
  672. explicit OutputFromIdentifier(PadIdentifier identifier_, InputEngine* input_engine_)
  673. : identifier(identifier_), input_engine(input_engine_) {}
  674. void SetLED(const Common::Input::LedStatus& led_status) override {
  675. input_engine->SetLeds(identifier, led_status);
  676. }
  677. Common::Input::VibrationError SetVibration(
  678. const Common::Input::VibrationStatus& vibration_status) override {
  679. return input_engine->SetVibration(identifier, vibration_status);
  680. }
  681. bool IsVibrationEnabled() override {
  682. return input_engine->IsVibrationEnabled(identifier);
  683. }
  684. Common::Input::PollingError SetPollingMode(Common::Input::PollingMode polling_mode) override {
  685. return input_engine->SetPollingMode(identifier, polling_mode);
  686. }
  687. Common::Input::CameraError SetCameraFormat(Common::Input::CameraFormat camera_format) override {
  688. return input_engine->SetCameraFormat(identifier, camera_format);
  689. }
  690. Common::Input::NfcState SupportsNfc() const override {
  691. return input_engine->SupportsNfc(identifier);
  692. }
  693. Common::Input::NfcState WriteNfcData(const std::vector<u8>& data) override {
  694. return input_engine->WriteNfcData(identifier, data);
  695. }
  696. private:
  697. const PadIdentifier identifier;
  698. InputEngine* input_engine;
  699. };
  700. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateButtonDevice(
  701. const Common::ParamPackage& params) {
  702. const PadIdentifier identifier = {
  703. .guid = Common::UUID{params.Get("guid", "")},
  704. .port = static_cast<std::size_t>(params.Get("port", 0)),
  705. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  706. };
  707. const auto button_id = params.Get("button", 0);
  708. const auto keyboard_key = params.Get("code", 0);
  709. const auto toggle = params.Get("toggle", false);
  710. const auto inverted = params.Get("inverted", false);
  711. input_engine->PreSetController(identifier);
  712. input_engine->PreSetButton(identifier, button_id);
  713. input_engine->PreSetButton(identifier, keyboard_key);
  714. if (keyboard_key != 0) {
  715. return std::make_unique<InputFromButton>(identifier, keyboard_key, toggle, inverted,
  716. input_engine.get());
  717. }
  718. return std::make_unique<InputFromButton>(identifier, button_id, toggle, inverted,
  719. input_engine.get());
  720. }
  721. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateHatButtonDevice(
  722. const Common::ParamPackage& params) {
  723. const PadIdentifier identifier = {
  724. .guid = Common::UUID{params.Get("guid", "")},
  725. .port = static_cast<std::size_t>(params.Get("port", 0)),
  726. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  727. };
  728. const auto button_id = params.Get("hat", 0);
  729. const auto direction = input_engine->GetHatButtonId(params.Get("direction", ""));
  730. const auto toggle = params.Get("toggle", false);
  731. const auto inverted = params.Get("inverted", false);
  732. input_engine->PreSetController(identifier);
  733. input_engine->PreSetHatButton(identifier, button_id);
  734. return std::make_unique<InputFromHatButton>(identifier, button_id, direction, toggle, inverted,
  735. input_engine.get());
  736. }
  737. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateStickDevice(
  738. const Common::ParamPackage& params) {
  739. const auto deadzone = std::clamp(params.Get("deadzone", 0.15f), 0.0f, 1.0f);
  740. const auto range = std::clamp(params.Get("range", 0.95f), 0.25f, 1.50f);
  741. const auto threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f);
  742. const PadIdentifier identifier = {
  743. .guid = Common::UUID{params.Get("guid", "")},
  744. .port = static_cast<std::size_t>(params.Get("port", 0)),
  745. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  746. };
  747. const auto axis_x = params.Get("axis_x", 0);
  748. const Common::Input::AnalogProperties properties_x = {
  749. .deadzone = deadzone,
  750. .range = range,
  751. .threshold = threshold,
  752. .offset = std::clamp(params.Get("offset_x", 0.0f), -1.0f, 1.0f),
  753. .inverted = params.Get("invert_x", "+") == "-",
  754. };
  755. const auto axis_y = params.Get("axis_y", 1);
  756. const Common::Input::AnalogProperties properties_y = {
  757. .deadzone = deadzone,
  758. .range = range,
  759. .threshold = threshold,
  760. .offset = std::clamp(params.Get("offset_y", 0.0f), -1.0f, 1.0f),
  761. .inverted = params.Get("invert_y", "+") != "+",
  762. };
  763. input_engine->PreSetController(identifier);
  764. input_engine->PreSetAxis(identifier, axis_x);
  765. input_engine->PreSetAxis(identifier, axis_y);
  766. return std::make_unique<InputFromStick>(identifier, axis_x, axis_y, properties_x, properties_y,
  767. input_engine.get());
  768. }
  769. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateAnalogDevice(
  770. const Common::ParamPackage& params) {
  771. const PadIdentifier identifier = {
  772. .guid = Common::UUID{params.Get("guid", "")},
  773. .port = static_cast<std::size_t>(params.Get("port", 0)),
  774. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  775. };
  776. const auto axis = params.Get("axis", 0);
  777. const Common::Input::AnalogProperties properties = {
  778. .deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f),
  779. .range = std::clamp(params.Get("range", 1.0f), 0.25f, 1.50f),
  780. .threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f),
  781. .offset = std::clamp(params.Get("offset", 0.0f), -1.0f, 1.0f),
  782. .inverted = params.Get("invert", "+") == "-",
  783. .toggle = static_cast<bool>(params.Get("toggle", false)),
  784. };
  785. input_engine->PreSetController(identifier);
  786. input_engine->PreSetAxis(identifier, axis);
  787. return std::make_unique<InputFromAnalog>(identifier, axis, properties, input_engine.get());
  788. }
  789. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateTriggerDevice(
  790. const Common::ParamPackage& params) {
  791. const PadIdentifier identifier = {
  792. .guid = Common::UUID{params.Get("guid", "")},
  793. .port = static_cast<std::size_t>(params.Get("port", 0)),
  794. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  795. };
  796. const auto button = params.Get("button", 0);
  797. const auto toggle = params.Get("toggle", false);
  798. const auto inverted = params.Get("inverted", false);
  799. const auto axis = params.Get("axis", 0);
  800. const Common::Input::AnalogProperties properties = {
  801. .deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f),
  802. .range = std::clamp(params.Get("range", 1.0f), 0.25f, 2.50f),
  803. .threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f),
  804. .offset = std::clamp(params.Get("offset", 0.0f), -1.0f, 1.0f),
  805. .inverted = params.Get("invert", false) != 0,
  806. };
  807. input_engine->PreSetController(identifier);
  808. input_engine->PreSetAxis(identifier, axis);
  809. input_engine->PreSetButton(identifier, button);
  810. return std::make_unique<InputFromTrigger>(identifier, button, toggle, inverted, axis,
  811. properties, input_engine.get());
  812. }
  813. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateTouchDevice(
  814. const Common::ParamPackage& params) {
  815. const auto touch_id = params.Get("touch_id", 0);
  816. const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f);
  817. const auto range = std::clamp(params.Get("range", 1.0f), 0.25f, 1.50f);
  818. const auto threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f);
  819. const PadIdentifier identifier = {
  820. .guid = Common::UUID{params.Get("guid", "")},
  821. .port = static_cast<std::size_t>(params.Get("port", 0)),
  822. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  823. };
  824. const auto button = params.Get("button", 0);
  825. const auto toggle = params.Get("toggle", false);
  826. const auto inverted = params.Get("inverted", false);
  827. const auto axis_x = params.Get("axis_x", 0);
  828. const Common::Input::AnalogProperties properties_x = {
  829. .deadzone = deadzone,
  830. .range = range,
  831. .threshold = threshold,
  832. .offset = std::clamp(params.Get("offset_x", 0.0f), -1.0f, 1.0f),
  833. .inverted = params.Get("invert_x", "+") == "-",
  834. };
  835. const auto axis_y = params.Get("axis_y", 1);
  836. const Common::Input::AnalogProperties properties_y = {
  837. .deadzone = deadzone,
  838. .range = range,
  839. .threshold = threshold,
  840. .offset = std::clamp(params.Get("offset_y", 0.0f), -1.0f, 1.0f),
  841. .inverted = params.Get("invert_y", false) != 0,
  842. };
  843. input_engine->PreSetController(identifier);
  844. input_engine->PreSetAxis(identifier, axis_x);
  845. input_engine->PreSetAxis(identifier, axis_y);
  846. input_engine->PreSetButton(identifier, button);
  847. return std::make_unique<InputFromTouch>(identifier, touch_id, button, toggle, inverted, axis_x,
  848. axis_y, properties_x, properties_y, input_engine.get());
  849. }
  850. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateBatteryDevice(
  851. const Common::ParamPackage& params) {
  852. const PadIdentifier identifier = {
  853. .guid = Common::UUID{params.Get("guid", "")},
  854. .port = static_cast<std::size_t>(params.Get("port", 0)),
  855. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  856. };
  857. input_engine->PreSetController(identifier);
  858. return std::make_unique<InputFromBattery>(identifier, input_engine.get());
  859. }
  860. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateMotionDevice(
  861. Common::ParamPackage params) {
  862. const PadIdentifier identifier = {
  863. .guid = Common::UUID{params.Get("guid", "")},
  864. .port = static_cast<std::size_t>(params.Get("port", 0)),
  865. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  866. };
  867. if (params.Has("motion")) {
  868. const auto motion_sensor = params.Get("motion", 0);
  869. const auto gyro_threshold = params.Get("threshold", 0.007f);
  870. input_engine->PreSetController(identifier);
  871. input_engine->PreSetMotion(identifier, motion_sensor);
  872. return std::make_unique<InputFromMotion>(identifier, motion_sensor, gyro_threshold,
  873. input_engine.get());
  874. }
  875. const auto deadzone = std::clamp(params.Get("deadzone", 0.15f), 0.0f, 1.0f);
  876. const auto range = std::clamp(params.Get("range", 1.0f), 0.25f, 1.50f);
  877. const auto threshold = std::clamp(params.Get("threshold", 0.5f), 0.0f, 1.0f);
  878. const auto axis_x = params.Get("axis_x", 0);
  879. const Common::Input::AnalogProperties properties_x = {
  880. .deadzone = deadzone,
  881. .range = range,
  882. .threshold = threshold,
  883. .offset = std::clamp(params.Get("offset_x", 0.0f), -1.0f, 1.0f),
  884. .inverted = params.Get("invert_x", "+") == "-",
  885. };
  886. const auto axis_y = params.Get("axis_y", 1);
  887. const Common::Input::AnalogProperties properties_y = {
  888. .deadzone = deadzone,
  889. .range = range,
  890. .threshold = threshold,
  891. .offset = std::clamp(params.Get("offset_y", 0.0f), -1.0f, 1.0f),
  892. .inverted = params.Get("invert_y", "+") != "+",
  893. };
  894. const auto axis_z = params.Get("axis_z", 1);
  895. const Common::Input::AnalogProperties properties_z = {
  896. .deadzone = deadzone,
  897. .range = range,
  898. .threshold = threshold,
  899. .offset = std::clamp(params.Get("offset_z", 0.0f), -1.0f, 1.0f),
  900. .inverted = params.Get("invert_z", "+") != "+",
  901. };
  902. input_engine->PreSetController(identifier);
  903. input_engine->PreSetAxis(identifier, axis_x);
  904. input_engine->PreSetAxis(identifier, axis_y);
  905. input_engine->PreSetAxis(identifier, axis_z);
  906. return std::make_unique<InputFromAxisMotion>(identifier, axis_x, axis_y, axis_z, properties_x,
  907. properties_y, properties_z, input_engine.get());
  908. }
  909. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateCameraDevice(
  910. const Common::ParamPackage& params) {
  911. const PadIdentifier identifier = {
  912. .guid = Common::UUID{params.Get("guid", "")},
  913. .port = static_cast<std::size_t>(params.Get("port", 0)),
  914. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  915. };
  916. input_engine->PreSetController(identifier);
  917. return std::make_unique<InputFromCamera>(identifier, input_engine.get());
  918. }
  919. std::unique_ptr<Common::Input::InputDevice> InputFactory::CreateNfcDevice(
  920. const Common::ParamPackage& params) {
  921. const PadIdentifier identifier = {
  922. .guid = Common::UUID{params.Get("guid", "")},
  923. .port = static_cast<std::size_t>(params.Get("port", 0)),
  924. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  925. };
  926. input_engine->PreSetController(identifier);
  927. return std::make_unique<InputFromNfc>(identifier, input_engine.get());
  928. }
  929. InputFactory::InputFactory(std::shared_ptr<InputEngine> input_engine_)
  930. : input_engine(std::move(input_engine_)) {}
  931. std::unique_ptr<Common::Input::InputDevice> InputFactory::Create(
  932. const Common::ParamPackage& params) {
  933. if (params.Has("battery")) {
  934. return CreateBatteryDevice(params);
  935. }
  936. if (params.Has("camera")) {
  937. return CreateCameraDevice(params);
  938. }
  939. if (params.Has("nfc")) {
  940. return CreateNfcDevice(params);
  941. }
  942. if (params.Has("button") && params.Has("axis")) {
  943. return CreateTriggerDevice(params);
  944. }
  945. if (params.Has("button") && params.Has("axis_x") && params.Has("axis_y")) {
  946. return CreateTouchDevice(params);
  947. }
  948. if (params.Has("button") || params.Has("code")) {
  949. return CreateButtonDevice(params);
  950. }
  951. if (params.Has("hat")) {
  952. return CreateHatButtonDevice(params);
  953. }
  954. if (params.Has("axis_x") && params.Has("axis_y") && params.Has("axis_z")) {
  955. return CreateMotionDevice(params);
  956. }
  957. if (params.Has("motion")) {
  958. return CreateMotionDevice(params);
  959. }
  960. if (params.Has("axis_x") && params.Has("axis_y")) {
  961. return CreateStickDevice(params);
  962. }
  963. if (params.Has("axis")) {
  964. return CreateAnalogDevice(params);
  965. }
  966. LOG_ERROR(Input, "Invalid parameters given");
  967. return std::make_unique<DummyInput>();
  968. }
  969. OutputFactory::OutputFactory(std::shared_ptr<InputEngine> input_engine_)
  970. : input_engine(std::move(input_engine_)) {}
  971. std::unique_ptr<Common::Input::OutputDevice> OutputFactory::Create(
  972. const Common::ParamPackage& params) {
  973. const PadIdentifier identifier = {
  974. .guid = Common::UUID{params.Get("guid", "")},
  975. .port = static_cast<std::size_t>(params.Get("port", 0)),
  976. .pad = static_cast<std::size_t>(params.Get("pad", 0)),
  977. };
  978. input_engine->PreSetController(identifier);
  979. return std::make_unique<OutputFromIdentifier>(identifier, input_engine.get());
  980. }
  981. } // namespace InputCommon