input_poller.cpp 42 KB

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