app.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <furi_hal_usb.h>
  4. #include <applications/services/power/power_service/power.h>
  5. #include <usb.h>
  6. #include <usb_hid.h>
  7. #include <stdlib.h>
  8. #include <gui/gui.h>
  9. #include <input/input.h>
  10. /* generated by fbt from .png files in images folder */
  11. #include <xinput_controller_icons.h>
  12. #define XBOX_SURFACE_EP_IN 0x81 // IN Endpoint 1
  13. #define HID_EP_SZ 0x10
  14. typedef struct {
  15. uint8_t x, y;
  16. } Position;
  17. enum {
  18. UP = 1 << 0,
  19. DOWN = 1 << 1,
  20. LEFT = 1 << 2,
  21. RIGHT = 1 << 3,
  22. START = 1 << 4,
  23. BACK = 1 << 5,
  24. L3 = 1 << 6,
  25. R3 = 1 << 7,
  26. LEFT_BUMPER = 1 << 8,
  27. RIGHT_BUMPER = 1 << 9,
  28. LOGO = 1 << 10,
  29. THE_VOID_ONE = 1 << 11, // https://www.partsnotincluded.com/wp-content/uploads/2019/03/X360_ButtonPackets.jpg
  30. A = 1 << 12,
  31. B = 1 << 13,
  32. X = 1 << 14,
  33. Y = 1 << 15,
  34. } xbox_buttons;
  35. // "shit" variables are unknown variables. I couldn't find any other way to add those. /shrug
  36. struct xbox_control_surface {
  37. // For the buttons, 0 means not pressed and 1 means pressed
  38. uint8_t message_type; // 0x00
  39. uint8_t length; // 20
  40. uint16_t buttons; // Refer to xbox_buttons enum for flags
  41. uint8_t left_trigger; // 0-255
  42. uint8_t right_trigger;
  43. int16_t left_x;
  44. int16_t left_y;
  45. int16_t right_x;
  46. int16_t right_y;
  47. uint8_t shit[6];
  48. } FURI_PACKED;
  49. struct xbox_control_surface current_surface;
  50. struct usb_unknown0_descriptor {
  51. uint8_t bLength;
  52. uint8_t bDescriptorType;
  53. uint8_t shit1, shit2, shit3, shit4; // 0x00, 0x01, 0x01, 0x25
  54. uint8_t bEndpointAddress; // 0x81 IN endpoint 1
  55. uint8_t bMaxDataSize;
  56. uint8_t shit5, shit6, shit7, shit8, shit9; // 0x00, 0x00, 0x00, 0x00, 0x13
  57. uint8_t bEndpointAddress2; // 0x01 OUT endpoint 1
  58. uint8_t bMaxDataSize2;
  59. uint8_t shit10, shit11; // 0x00, 0x00
  60. };
  61. struct usb_unknown1_descriptor {
  62. uint8_t bLength;
  63. uint8_t bDescriptorType;
  64. uint8_t shit1, shit2, shit3, shit4;
  65. uint8_t bEndpointAddress;
  66. uint8_t bMaxDataSize;
  67. uint8_t shit5;
  68. uint8_t bEndpointAddress2;
  69. uint8_t bMaxDataSize2;
  70. uint8_t shit6;
  71. uint8_t bEndpointAddress3;
  72. uint8_t bMaxDataSize3;
  73. uint8_t shit7, shit8, shit9, shit10, shit11, shit12;
  74. uint8_t bEndpointAddress4;
  75. uint8_t bMaxDataSize4;
  76. uint8_t shit13, shit14, shit15, shit16, shit17;
  77. };
  78. struct usb_unknown2_descriptor {
  79. uint8_t bLength;
  80. uint8_t bDescriptorType;
  81. uint8_t shit1, shit2, shit3, shit4;
  82. uint8_t bEndpointAddress;
  83. uint8_t bMaxDataSize;
  84. uint8_t shit5;
  85. };
  86. struct usb_unknown3_descriptor {
  87. uint8_t bLength;
  88. uint8_t bDescriptorType;
  89. uint8_t shit1, shit2, shit3, shit4;
  90. };
  91. struct XboxIntf0Descriptor {
  92. struct usb_interface_descriptor hid;
  93. struct usb_unknown0_descriptor unknown;
  94. struct usb_endpoint_descriptor hid_ep_in;
  95. struct usb_endpoint_descriptor hid_ep_out;
  96. };
  97. struct XboxIntf1Descriptor {
  98. struct usb_interface_descriptor hid;
  99. struct usb_unknown1_descriptor unknown;
  100. struct usb_endpoint_descriptor hid_ep_in_2;
  101. struct usb_endpoint_descriptor hid_ep_out_2;
  102. struct usb_endpoint_descriptor hid_ep_in_3;
  103. struct usb_endpoint_descriptor hid_ep_out_3;
  104. };
  105. struct XboxIntf2Descriptor {
  106. struct usb_interface_descriptor hid;
  107. struct usb_unknown2_descriptor unknown;
  108. struct usb_endpoint_descriptor hid_ep_in_4;
  109. };
  110. struct XboxIntf3Descriptor {
  111. struct usb_interface_descriptor hid;
  112. struct usb_unknown3_descriptor unknown;
  113. };
  114. struct XboxConfigDescriptor {
  115. struct usb_config_descriptor config;
  116. struct XboxIntf0Descriptor intf_0;
  117. struct XboxIntf1Descriptor intf_1;
  118. struct XboxIntf2Descriptor intf_2;
  119. struct XboxIntf3Descriptor intf_3;
  120. } FURI_PACKED;
  121. static struct usb_device_descriptor xbox_device_desc = {
  122. .bLength = 0x12,
  123. .bDescriptorType = 0x01,
  124. .bcdUSB = 0x0200,
  125. .bDeviceClass = 0xFF, // Vendor specific
  126. .bDeviceSubClass = 0xFF,
  127. .bDeviceProtocol = 0xFF,
  128. .bMaxPacketSize0 = 0x08,
  129. .idVendor = 0x045E,
  130. .idProduct = 0x028E,
  131. .bcdDevice = 0x0114,
  132. .iManufacturer = 0x01,
  133. .iProduct = 0x02,
  134. .iSerialNumber = 0x03,
  135. .bNumConfigurations = 0x01,
  136. };
  137. static const struct XboxConfigDescriptor xbox_cfg_desc = {
  138. .config = {
  139. .bLength = 9,
  140. .bDescriptorType = 0x02, // CONFIGURATION
  141. .wTotalLength = 153,
  142. .bNumInterfaces = 4,
  143. .bConfigurationValue = 1,
  144. .iConfiguration = 0,
  145. .bmAttributes = 0b10100000, // Not self-powered, remote wake-up
  146. .bMaxPower = USB_CFG_POWER_MA(500),
  147. },
  148. .intf_0 = {
  149. .hid = {
  150. .bLength = 9,
  151. .bDescriptorType = 0x04, // INTERFACE
  152. .bInterfaceNumber = 0,
  153. .bAlternateSetting = 0,
  154. .bNumEndpoints = 2,
  155. .bInterfaceClass = 0xFF, // Vendor Specific
  156. .bInterfaceSubClass = 0x5D,
  157. .bInterfaceProtocol = 0x01,
  158. .iInterface = 0,
  159. },
  160. .unknown = { // Unknown descriptor
  161. .bLength = 17,
  162. .bDescriptorType = 0x21, // UNKNOWN
  163. .shit1 = 0x00, .shit2 = 0x01, .shit3 = 0x01,
  164. .shit4 = 0x25,
  165. .bEndpointAddress = 0x81, // IN Endpoint 1
  166. .bMaxDataSize = 20,
  167. .shit5 = 0x00, .shit6 = 0x00, .shit7 = 0x00, .shit8 = 0x00, .shit9 = 0x13,
  168. .bEndpointAddress2 = 0x01, // OUT Endpoint 1
  169. .bMaxDataSize2 = 8,
  170. .shit10 = 0x00, .shit11 = 0x00,
  171. },
  172. .hid_ep_in = {
  173. .bLength = 7,
  174. .bDescriptorType = 0x05, // ENDPOINT
  175. .bEndpointAddress = 0x81, // IN Endpoint 1
  176. .bmAttributes = 0x03,
  177. .wMaxPacketSize = 32,
  178. .bInterval = 4,
  179. },
  180. .hid_ep_out = {
  181. .bLength = 7,
  182. .bDescriptorType = 0x05, // ENDPOINT
  183. .bEndpointAddress = 0x01, // OUT Endpoint 1
  184. .bmAttributes = 0x03,
  185. .wMaxPacketSize = 32,
  186. .bInterval = 8,
  187. }
  188. },
  189. .intf_1 = {
  190. .hid = {
  191. .bLength = 9,
  192. .bDescriptorType = 0x04, // INTERFACE
  193. .bInterfaceNumber = 1,
  194. .bAlternateSetting = 0,
  195. .bNumEndpoints = 4,
  196. .bInterfaceClass = 0xFF, // Vendor Specific
  197. .bInterfaceSubClass = 0x5D,
  198. .bInterfaceProtocol = 0x03,
  199. .iInterface = 0,
  200. },
  201. .unknown = {
  202. .bLength = 27,
  203. .bDescriptorType = 0x21, // UNKNOWN
  204. .shit1 = 0x00,
  205. .shit2 = 0x01,
  206. .shit3 = 0x01,
  207. .shit4 = 0x01,
  208. .bEndpointAddress = 0x82, // IN Endpoint 2
  209. .bMaxDataSize = 64,
  210. .shit5 = 0x01,
  211. .bEndpointAddress2 = 0x02, // OUT Endpoint 2
  212. .bMaxDataSize2 = 32,
  213. .shit6 = 0x16,
  214. .bEndpointAddress3 = 0x83, // IN Endpoint 3
  215. .bMaxDataSize3 = 0,
  216. .shit7 = 0x00,
  217. .shit8 = 0x00,
  218. .shit9 = 0x00,
  219. .shit10 = 0x00,
  220. .shit11 = 0x00,
  221. .shit12 = 0x16,
  222. .bEndpointAddress4 = 0x03, // OUT Endpoint 3
  223. .bMaxDataSize4 = 0,
  224. .shit13 = 0x00,
  225. .shit14 = 0x00,
  226. .shit15 = 0x00,
  227. .shit16 = 0x00,
  228. .shit17 = 0x00,
  229. },
  230. .hid_ep_in_2 = {
  231. .bLength = 7,
  232. .bDescriptorType = 0x05, // ENDPOINT
  233. .bEndpointAddress = 0x82, // IN Endpoint 2
  234. .bmAttributes = 0x03,
  235. .wMaxPacketSize = 32,
  236. .bInterval = 2,
  237. },
  238. .hid_ep_out_2 = {
  239. .bLength = 7,
  240. .bDescriptorType = 0x05, // ENDPOINT
  241. .bEndpointAddress = 0x02, // OUT Endpoint 2
  242. .bmAttributes = 0x03,
  243. .wMaxPacketSize = 32,
  244. .bInterval = 4,
  245. },
  246. .hid_ep_in_3 = {
  247. .bLength = 7,
  248. .bDescriptorType = 0x05, // ENDPOINT
  249. .bEndpointAddress = 0x83, // IN Endpoint 3
  250. .bmAttributes = 0x03,
  251. .wMaxPacketSize = 32,
  252. .bInterval = 64,
  253. },
  254. .hid_ep_out_3 = {
  255. .bLength = 7,
  256. .bDescriptorType = 0x05, // ENDPOINT
  257. .bEndpointAddress = 0x03, // OUT Endpoint 3
  258. .bmAttributes = 0x03,
  259. .wMaxPacketSize = 32,
  260. .bInterval = 16,
  261. },
  262. },
  263. .intf_2 = {
  264. .hid = {
  265. .bLength = 9,
  266. .bDescriptorType = 0x04, // INTERFACE
  267. .bInterfaceNumber = 2,
  268. .bAlternateSetting = 0,
  269. .bNumEndpoints = 1,
  270. .bInterfaceClass = 0xFF, // Vendor Specific
  271. .bInterfaceSubClass = 0x5D,
  272. .bInterfaceProtocol = 0x02,
  273. .iInterface = 0,
  274. },
  275. .unknown = {
  276. .bLength = 9,
  277. .bDescriptorType = 0x21, // UNKNOWN
  278. .shit1 = 0x00,
  279. .shit2 = 0x01,
  280. .shit3 = 0x01,
  281. .shit4 = 0x22,
  282. .bEndpointAddress = 0x84, // IN Endpoint 4
  283. .bMaxDataSize = 7,
  284. .shit5 = 0x00,
  285. },
  286. .hid_ep_in_4 = {
  287. .bLength = 7,
  288. .bDescriptorType = 0x05, // ENDPOINT
  289. .bEndpointAddress = 0x84, // IN Endpoint 4
  290. .bmAttributes = 0x03,
  291. .wMaxPacketSize = 32,
  292. .bInterval = 16,
  293. }
  294. },
  295. .intf_3 = {
  296. .hid = {
  297. .bLength = 9,
  298. .bDescriptorType = 0x04, // INTERFACE
  299. .bInterfaceNumber = 3,
  300. .bAlternateSetting = 0,
  301. .bNumEndpoints = 0,
  302. .bInterfaceClass = 0xFF, // Vendor Specific
  303. .bInterfaceSubClass = 0xFD,
  304. .bInterfaceProtocol = 0x13,
  305. .iInterface = 4,
  306. },
  307. .unknown = {
  308. .bLength = 6,
  309. .bDescriptorType = 0x41, // UNKNOWN
  310. .shit1 = 0x00,
  311. .shit2 = 0x01,
  312. .shit3 = 0x01,
  313. .shit4 = 0x03,
  314. },
  315. },
  316. };
  317. static bool boot_protocol = false;
  318. static usbd_device* usb_dev;
  319. static FuriSemaphore* hid_semaphore = NULL;
  320. static bool hid_connected = false;
  321. static void* hid_set_string_descr(char* str) {
  322. furi_assert(str);
  323. size_t len = strlen(str);
  324. struct usb_string_descriptor* dev_str_desc = malloc(len * 2 + 2);
  325. dev_str_desc->bLength = len * 2 + 2;
  326. dev_str_desc->bDescriptorType = USB_DTYPE_STRING;
  327. for(size_t i = 0; i < len; i++)
  328. dev_str_desc->wString[i] = str[i];
  329. return dev_str_desc;
  330. }
  331. static void hid_txrx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  332. UNUSED(dev);
  333. UNUSED(ep);
  334. if(event == usbd_evt_eptx) {
  335. furi_semaphore_release(hid_semaphore);
  336. } else if(boot_protocol == true) {
  337. //uint8_t message_type;
  338. //usbd_ep_read(usb_dev, ep, &led_state, sizeof(led_state));
  339. } else {
  340. //struct HidReportLED leds;
  341. //usbd_ep_read(usb_dev, ep, &leds, sizeof(leds));
  342. //led_state = leds.led_state;
  343. }
  344. }
  345. static usbd_respond hid_ep_config(usbd_device* dev, uint8_t cfg) {
  346. switch(cfg) {
  347. case 0:
  348. /* deconfiguring device */
  349. usbd_ep_deconfig(dev, XBOX_SURFACE_EP_IN);
  350. usbd_reg_endpoint(dev, XBOX_SURFACE_EP_IN, 0);
  351. return usbd_ack;
  352. case 1:
  353. /* configuring device */
  354. usbd_ep_config(dev, XBOX_SURFACE_EP_IN, USB_EPTYPE_INTERRUPT, 32);
  355. usbd_reg_endpoint(dev, XBOX_SURFACE_EP_IN, hid_txrx_ep_callback);
  356. //usbd_ep_write(dev, HID_EP_IN, 0, 0);
  357. boot_protocol = false; /* BIOS will SET_PROTOCOL if it wants this */
  358. return usbd_ack;
  359. default:
  360. return usbd_fail;
  361. }
  362. }
  363. static usbd_respond hid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
  364. UNUSED(callback);
  365. /* HID control requests */
  366. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  367. (USB_REQ_INTERFACE | USB_REQ_CLASS) &&
  368. req->wIndex == 0) {
  369. switch(req->bRequest) {
  370. case USB_HID_SETIDLE:
  371. return usbd_ack;
  372. case USB_HID_GETREPORT:
  373. //if(boot_protocol == true) {
  374. // dev->status.data_ptr = &hid_report.keyboard.boot;
  375. // dev->status.data_count = sizeof(hid_report.keyboard.boot);
  376. //} else {
  377. // dev->status.data_ptr = &hid_report;
  378. // dev->status.data_count = sizeof(hid_report);
  379. //}
  380. return usbd_ack;
  381. case USB_HID_SETPROTOCOL:
  382. if(req->wValue == 0)
  383. boot_protocol = true;
  384. else if(req->wValue == 1)
  385. boot_protocol = false;
  386. else
  387. return usbd_fail;
  388. return usbd_ack;
  389. default:
  390. return usbd_fail;
  391. }
  392. }
  393. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  394. (USB_REQ_INTERFACE | USB_REQ_STANDARD) &&
  395. req->wIndex == 0 && req->bRequest == USB_STD_GET_DESCRIPTOR) {
  396. switch(req->wValue >> 8) {
  397. case USB_DTYPE_HID:
  398. dev->status.data_ptr = (uint8_t*)&(xbox_cfg_desc.intf_0.hid);
  399. dev->status.data_count = sizeof(xbox_cfg_desc.intf_0.hid);
  400. return usbd_ack;
  401. default:
  402. return usbd_fail;
  403. }
  404. }
  405. return usbd_fail;
  406. }
  407. static void hid_deinit(usbd_device* dev) {
  408. usbd_reg_config(dev, NULL);
  409. usbd_reg_control(dev, NULL);
  410. }
  411. static void hid_on_wakeup(usbd_device* dev) {
  412. UNUSED(dev);
  413. if(!hid_connected) {
  414. hid_connected = true;
  415. }
  416. }
  417. static void hid_on_suspend(usbd_device* dev) {
  418. UNUSED(dev);
  419. if(hid_connected) {
  420. hid_connected = false;
  421. furi_semaphore_release(hid_semaphore);
  422. }
  423. }
  424. static bool hid_send_report() {
  425. if((hid_semaphore == NULL) || (hid_connected == false)) return false;
  426. FuriStatus status = furi_semaphore_acquire(hid_semaphore, 8 * 2);
  427. if(status == FuriStatusErrorTimeout) {
  428. return false;
  429. }
  430. furi_check(status == FuriStatusOk);
  431. if(hid_connected == false) {
  432. return false;
  433. }
  434. if (boot_protocol != true) {
  435. usbd_ep_write(usb_dev, XBOX_SURFACE_EP_IN, &current_surface, sizeof(current_surface));
  436. }
  437. return true;
  438. }
  439. static void hid_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx);
  440. FuriHalUsbInterface usb_xbox = {
  441. .init = hid_init,
  442. .deinit = hid_deinit,
  443. .wakeup = hid_on_wakeup,
  444. .suspend = hid_on_suspend,
  445. .dev_descr = (struct usb_device_descriptor*)&xbox_device_desc,
  446. .str_manuf_descr = NULL, //USB_STRING_DESC("Microsoft Corporation"),
  447. .str_prod_descr = NULL, //USB_STRING_DESC("Controller"),
  448. .str_serial_descr = NULL, //USB_STRING_DESC("08FEC93"),
  449. .cfg_descr = (void*)&xbox_cfg_desc
  450. };
  451. static void hid_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
  452. UNUSED(intf);
  453. UNUSED(ctx);
  454. // FuriHalUsbHidConfig* cfg = (FuriHalUsbHidConfig*)ctx;
  455. // UNUSED(cfg);
  456. if(hid_semaphore == NULL) hid_semaphore = furi_semaphore_alloc(1, 1);
  457. usb_dev = dev;
  458. usb_xbox.dev_descr->iManufacturer = 1;
  459. usb_xbox.dev_descr->iProduct = 2;
  460. usb_xbox.dev_descr->iSerialNumber = 3;
  461. usb_xbox.dev_descr->idVendor = 0x045E; // Microsoft
  462. usb_xbox.dev_descr->idProduct = 0x028E; // Xbox 360 Controller
  463. usb_xbox.str_manuf_descr = hid_set_string_descr("Microsoft Corporation");
  464. usb_xbox.str_prod_descr = hid_set_string_descr("Controller");
  465. usb_xbox.str_serial_descr = hid_set_string_descr("08FEC93");
  466. usbd_reg_config(dev, hid_ep_config);
  467. usbd_reg_control(dev, hid_control);
  468. usbd_connect(dev, true);
  469. }
  470. char* message;
  471. static void app_draw_callback(Canvas* canvas, void* ctx) {
  472. UNUSED(ctx);
  473. canvas_clear(canvas);
  474. canvas_set_color(canvas, ColorBlack);
  475. canvas_set_font(canvas, FontSecondary);
  476. if (!hid_connected) {
  477. canvas_set_font(canvas, FontPrimary);
  478. canvas_draw_str(canvas, 60, 35, "Connecting...");
  479. canvas_draw_icon(canvas, 5, 20, &I_usb);
  480. } else {
  481. canvas_set_font(canvas, FontPrimary);
  482. canvas_draw_str(canvas, 74, 12, "Controller");
  483. canvas_set_font(canvas, FontSecondary);
  484. canvas_draw_str(canvas, 64, 24, "Hold");
  485. canvas_draw_icon(canvas, 84, 16, &I_back);
  486. canvas_draw_str(canvas, 97, 24, "to exit");
  487. // Joystick up
  488. if (current_surface.left_y > 200) {
  489. canvas_draw_icon(canvas, 26, 13, &I_up_held);
  490. } else {
  491. canvas_draw_icon(canvas, 26, 13, &I_up);
  492. }
  493. // Joystick down
  494. if (current_surface.left_y < -200) {
  495. canvas_draw_icon(canvas, 26, 36, &I_down_held);
  496. } else {
  497. canvas_draw_icon(canvas, 26, 36, &I_down);
  498. }
  499. // Joystick left
  500. if (current_surface.left_x < -200) {
  501. canvas_draw_icon(canvas, 3, 33, &I_left_held);
  502. } else {
  503. canvas_draw_icon(canvas, 3, 33, &I_left);
  504. }
  505. // Joystick right
  506. if (current_surface.left_x > 200) {
  507. canvas_draw_icon(canvas, 52, 33, &I_right_held);
  508. } else {
  509. canvas_draw_icon(canvas, 52, 33, &I_right);
  510. }
  511. // A
  512. if (current_surface.buttons & A) {
  513. canvas_draw_icon(canvas, 79, 40, &I_a_held);
  514. } else {
  515. canvas_draw_icon(canvas, 79, 40, &I_a);
  516. }
  517. // B
  518. if (current_surface.buttons & B) {
  519. canvas_draw_icon(canvas, 100, 40, &I_b_held);
  520. } else {
  521. canvas_draw_icon(canvas, 100, 40, &I_b);
  522. }
  523. }
  524. }
  525. static void app_input_callback(InputEvent* input_event, void* ctx) {
  526. furi_assert(ctx);
  527. FuriMessageQueue* event_queue = ctx;
  528. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  529. }
  530. int32_t xinput_app(void* p) {
  531. UNUSED(p);
  532. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  533. ViewPort* view_port = view_port_alloc();
  534. view_port_draw_callback_set(view_port, app_draw_callback, view_port);
  535. view_port_input_callback_set(view_port, app_input_callback, event_queue);
  536. Gui* gui = furi_record_open(RECORD_GUI);
  537. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  538. InputEvent input_event;
  539. bool running = true;
  540. FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();
  541. furi_hal_usb_unlock();
  542. furi_check(furi_hal_usb_set_config(&usb_xbox, NULL));
  543. while (true) {
  544. while (!hid_connected) {
  545. view_port_update(view_port);
  546. }
  547. furi_check(furi_message_queue_get(event_queue, &input_event, FuriWaitForever) == FuriStatusOk);
  548. if (!running) {
  549. break;
  550. }
  551. message = strdup(input_get_type_name(input_event.type));
  552. if (input_event.type == InputTypeLong && input_event.key == InputKeyBack && current_surface.buttons == B) {
  553. running = false;
  554. }
  555. if (input_event.type == InputTypeRepeat) {
  556. // No
  557. } else if (input_event.type == InputTypePress) {
  558. switch (input_event.key) {
  559. case InputKeyLeft:
  560. current_surface.left_x = -32767;
  561. break;
  562. case InputKeyRight:
  563. current_surface.left_x = 32767;
  564. break;
  565. case InputKeyOk:
  566. current_surface.buttons = current_surface.buttons | A;
  567. break;
  568. case InputKeyUp:
  569. current_surface.left_y = 32767;
  570. break;
  571. case InputKeyDown:
  572. current_surface.left_y = -32767;
  573. break;
  574. case InputKeyBack:
  575. current_surface.buttons = current_surface.buttons | B;
  576. default:
  577. break;
  578. }
  579. }
  580. if (input_event.type == InputTypeRelease) {
  581. switch (input_event.key) {
  582. case InputKeyLeft:
  583. case InputKeyRight:
  584. current_surface.left_x = 0;
  585. break;
  586. case InputKeyOk:
  587. // current_surface.buttons = current_surface.buttons | THE_VOID_ONE;
  588. current_surface.buttons = current_surface.buttons & ~ A;
  589. break;
  590. case InputKeyBack:
  591. current_surface.buttons = current_surface.buttons & ~ B;
  592. break;
  593. case InputKeyUp:
  594. case InputKeyDown:
  595. current_surface.left_y = 0;
  596. break;
  597. default:
  598. break;
  599. }
  600. }
  601. view_port_update(view_port);
  602. hid_send_report();
  603. }
  604. furi_check(furi_hal_usb_set_config(usb_mode_prev, NULL));
  605. view_port_enabled_set(view_port, false);
  606. gui_remove_view_port(gui, view_port);
  607. view_port_free(view_port);
  608. furi_message_queue_free(event_queue);
  609. furi_record_close(RECORD_GUI);
  610. return 0;
  611. }