SDL_hidapi_lg4ff.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 2025 Simon Wood <simon@mungewell.org>
  4. Copyright (C) 2025 Michal Malý <madcatxster@devoid-pointer.net>
  5. Copyright (C) 2025 Katharine Chui <katharine.chui@gmail.com>
  6. This software is provided 'as-is', without any express or implied
  7. warranty. In no event will the authors be held liable for any damages
  8. arising from the use of this software.
  9. Permission is granted to anyone to use this software for any purpose,
  10. including commercial applications, and to alter it and redistribute it
  11. freely, subject to the following restrictions:
  12. 1. The origin of this software must not be misrepresented; you must not
  13. claim that you wrote the original software. If you use this software
  14. in a product, an acknowledgment in the product documentation would be
  15. appreciated but is not required.
  16. 2. Altered source versions must be plainly marked as such, and must not be
  17. misrepresented as being the original software.
  18. 3. This notice may not be removed or altered from any source distribution.
  19. */
  20. #include "SDL_internal.h"
  21. #ifdef SDL_JOYSTICK_HIDAPI
  22. #include "../SDL_sysjoystick.h"
  23. #include "SDL3/SDL_events.h"
  24. #include "SDL_hidapijoystick_c.h"
  25. #ifdef SDL_JOYSTICK_HIDAPI_LG4FF
  26. #define USB_VENDOR_ID_LOGITECH 0x046d
  27. #define USB_DEVICE_ID_LOGITECH_G29_WHEEL 0xc24f
  28. #define USB_DEVICE_ID_LOGITECH_G27_WHEEL 0xc29b
  29. #define USB_DEVICE_ID_LOGITECH_G25_WHEEL 0xc299
  30. #define USB_DEVICE_ID_LOGITECH_DFGT_WHEEL 0xc29a
  31. #define USB_DEVICE_ID_LOGITECH_DFP_WHEEL 0xc298
  32. #define USB_DEVICE_ID_LOGITECH_WHEEL 0xc294
  33. static Uint32 supported_device_ids[] = {
  34. USB_DEVICE_ID_LOGITECH_G29_WHEEL,
  35. USB_DEVICE_ID_LOGITECH_G27_WHEEL,
  36. USB_DEVICE_ID_LOGITECH_G25_WHEEL,
  37. USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
  38. USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
  39. USB_DEVICE_ID_LOGITECH_WHEEL
  40. };
  41. // keep the same order as the supported_ids array
  42. static const char *supported_device_names[] = {
  43. "Logitech G29",
  44. "Logitech G27",
  45. "Logitech G25",
  46. "Logitech Driving Force GT",
  47. "Logitech Driving Force Pro",
  48. "Driving Force EX"
  49. };
  50. static const char *HIDAPI_DriverLg4ff_GetDeviceName(Uint32 device_id)
  51. {
  52. for (int i = 0;i < (sizeof supported_device_ids) / sizeof(Uint32);i++) {
  53. if (supported_device_ids[i] == device_id) {
  54. return supported_device_names[i];
  55. }
  56. }
  57. SDL_assert(0);
  58. return "";
  59. }
  60. static int HIDAPI_DriverLg4ff_GetNumberOfButtons(Uint32 device_id)
  61. {
  62. switch (device_id) {
  63. case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
  64. return 25;
  65. case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
  66. return 23;
  67. case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
  68. return 19;
  69. case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
  70. return 21;
  71. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  72. return 14;
  73. case USB_DEVICE_ID_LOGITECH_WHEEL:
  74. return 13;
  75. default:
  76. SDL_assert(0);
  77. return 0;
  78. }
  79. }
  80. typedef struct
  81. {
  82. Uint8 last_report_buf[32];
  83. bool initialized;
  84. bool is_ffex;
  85. Uint16 range;
  86. } SDL_DriverLg4ff_Context;
  87. static void HIDAPI_DriverLg4ff_RegisterHints(SDL_HintCallback callback, void *userdata)
  88. {
  89. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_LG4FF, callback, userdata);
  90. }
  91. static void HIDAPI_DriverLg4ff_UnregisterHints(SDL_HintCallback callback, void *userdata)
  92. {
  93. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_LG4FF, callback, userdata);
  94. }
  95. static bool HIDAPI_DriverLg4ff_IsEnabled(void)
  96. {
  97. bool enabled = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_LG4FF,
  98. SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
  99. return enabled;
  100. }
  101. /*
  102. Wheel id information by:
  103. Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
  104. Simon Wood <simon@mungewell.org>
  105. `git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
  106. */
  107. static Uint16 HIDAPI_DriverLg4ff_IdentifyWheel(Uint16 device_id, Uint16 release_number)
  108. {
  109. #define is_device(ret, m, r) { \
  110. if ((release_number & m) == r) { \
  111. return ret; \
  112. } \
  113. }
  114. #define is_dfp { \
  115. is_device(USB_DEVICE_ID_LOGITECH_DFP_WHEEL, 0xf000, 0x1000); \
  116. }
  117. #define is_dfgt { \
  118. is_device(USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, 0xff00, 0x1300); \
  119. }
  120. #define is_g25 { \
  121. is_device(USB_DEVICE_ID_LOGITECH_G25_WHEEL, 0xff00, 0x1200); \
  122. }
  123. #define is_g27 { \
  124. is_device(USB_DEVICE_ID_LOGITECH_G27_WHEEL, 0xfff0, 0x1230); \
  125. }
  126. #define is_g29 { \
  127. is_device(USB_DEVICE_ID_LOGITECH_G29_WHEEL, 0xfff8, 0x1350); \
  128. is_device(USB_DEVICE_ID_LOGITECH_G29_WHEEL, 0xff00, 0x8900); \
  129. }
  130. switch(device_id){
  131. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  132. case USB_DEVICE_ID_LOGITECH_WHEEL:
  133. is_g29;
  134. is_g27;
  135. is_g25;
  136. is_dfgt;
  137. is_dfp;
  138. break;
  139. case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
  140. is_g29;
  141. is_dfgt;
  142. break;
  143. case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
  144. is_g29;
  145. is_g27;
  146. is_g25;
  147. break;
  148. case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
  149. is_g29;
  150. is_g27;
  151. break;
  152. case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
  153. is_g29;
  154. break;
  155. }
  156. return 0;
  157. #undef is_device
  158. #undef is_dfp
  159. #undef is_dfgt
  160. #undef is_g25
  161. #undef is_g27
  162. #undef is_g29
  163. }
  164. static int SDL_HIDAPI_DriverLg4ff_GetEnvInt(const char *env_name, int min, int max, int def)
  165. {
  166. const char *env = SDL_getenv(env_name);
  167. int value = 0;
  168. if(env == NULL) {
  169. return def;
  170. }
  171. value = SDL_atoi(env);
  172. if (value < min) {
  173. value = min;
  174. }
  175. if (value > max) {
  176. value = max;
  177. }
  178. return value;
  179. }
  180. /*
  181. Commands by:
  182. Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
  183. Simon Wood <simon@mungewell.org>
  184. `git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
  185. */
  186. static bool HIDAPI_DriverLg4ff_SwitchMode(SDL_HIDAPI_Device *device, Uint16 target_product_id){
  187. int ret = 0;
  188. switch(target_product_id){
  189. case USB_DEVICE_ID_LOGITECH_G29_WHEEL:{
  190. Uint8 cmd[] = {0xf8, 0x09, 0x05, 0x01, 0x01, 0x00, 0x00};
  191. ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
  192. break;
  193. }
  194. case USB_DEVICE_ID_LOGITECH_G27_WHEEL:{
  195. Uint8 cmd[] = {0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00};
  196. ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
  197. break;
  198. }
  199. case USB_DEVICE_ID_LOGITECH_G25_WHEEL:{
  200. Uint8 cmd[] = {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00};
  201. ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
  202. break;
  203. }
  204. case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:{
  205. Uint8 cmd[] = {0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00};
  206. ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
  207. break;
  208. }
  209. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:{
  210. Uint8 cmd[] = {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
  211. ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
  212. break;
  213. }
  214. case USB_DEVICE_ID_LOGITECH_WHEEL:{
  215. Uint8 cmd[] = {0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00};
  216. ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
  217. break;
  218. }
  219. default:{
  220. SDL_assert(0);
  221. }
  222. }
  223. if(ret == -1){
  224. return false;
  225. }
  226. return true;
  227. }
  228. static bool HIDAPI_DriverLg4ff_IsSupportedDevice(
  229. SDL_HIDAPI_Device *device,
  230. const char *name,
  231. SDL_GamepadType type,
  232. Uint16 vendor_id,
  233. Uint16 product_id,
  234. Uint16 version,
  235. int interface_number,
  236. int interface_class,
  237. int interface_subclass,
  238. int interface_protocol)
  239. {
  240. int i;
  241. if (vendor_id != USB_VENDOR_ID_LOGITECH) {
  242. return false;
  243. }
  244. for (i = 0;i < sizeof(supported_device_ids) / sizeof(Uint32);i++) {
  245. if (supported_device_ids[i] == product_id) {
  246. break;
  247. }
  248. }
  249. if (i == sizeof(supported_device_ids) / sizeof(Uint32)) {
  250. return false;
  251. }
  252. Uint16 real_id = HIDAPI_DriverLg4ff_IdentifyWheel(product_id, version);
  253. if (real_id == product_id || real_id == 0) {
  254. // either it is already in native mode, or we don't know what the native mode is
  255. return true;
  256. }
  257. // a supported native mode is found, send mode change command, then still state that we support the device
  258. if (device && SDL_HIDAPI_DriverLg4ff_GetEnvInt("SDL_HIDAPI_LG4FF_NO_MODE_SWITCH", 0, 1, 0) == 0) {
  259. HIDAPI_DriverLg4ff_SwitchMode(device, real_id);
  260. }
  261. return true;
  262. }
  263. /*
  264. *Ported*
  265. Original functions by:
  266. Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
  267. lg4ff_set_range_g25 lg4ff_set_range_dfp
  268. `git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
  269. */
  270. static bool HIDAPI_DriverLg4ff_SetRange(SDL_HIDAPI_Device *device, int range)
  271. {
  272. Uint8 cmd[7] = {0};
  273. int ret = 0;
  274. SDL_DriverLg4ff_Context *ctx = (SDL_DriverLg4ff_Context *)device->context;
  275. if (range < 40) {
  276. range = 40;
  277. }
  278. if (range > 900) {
  279. range = 900;
  280. }
  281. ctx->range = (Uint16)range;
  282. switch (device->product_id) {
  283. case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
  284. case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
  285. case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
  286. case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:{
  287. cmd[0] = 0xf8;
  288. cmd[1] = 0x81;
  289. cmd[2] = range & 0x00ff;
  290. cmd[3] = (range & 0xff00) >> 8;
  291. ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
  292. if (ret == -1) {
  293. return false;
  294. }
  295. break;
  296. }
  297. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:{
  298. int start_left, start_right, full_range;
  299. /* Prepare "coarse" limit command */
  300. cmd[0] = 0xf8;
  301. cmd[1] = 0x00; /* Set later */
  302. cmd[2] = 0x00;
  303. cmd[3] = 0x00;
  304. cmd[4] = 0x00;
  305. cmd[5] = 0x00;
  306. cmd[6] = 0x00;
  307. if (range > 200) {
  308. cmd[1] = 0x03;
  309. full_range = 900;
  310. } else {
  311. cmd[1] = 0x02;
  312. full_range = 200;
  313. }
  314. ret = SDL_hid_write(device->dev, cmd, 7);
  315. if(ret == -1){
  316. return false;
  317. }
  318. /* Prepare "fine" limit command */
  319. cmd[0] = 0x81;
  320. cmd[1] = 0x0b;
  321. cmd[2] = 0x00;
  322. cmd[3] = 0x00;
  323. cmd[4] = 0x00;
  324. cmd[5] = 0x00;
  325. cmd[6] = 0x00;
  326. if (range != 200 && range != 900) {
  327. /* Construct fine limit command */
  328. start_left = (((full_range - range + 1) * 2047) / full_range);
  329. start_right = 0xfff - start_left;
  330. cmd[2] = (Uint8)(start_left >> 4);
  331. cmd[3] = (Uint8)(start_right >> 4);
  332. cmd[4] = 0xff;
  333. cmd[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
  334. cmd[6] = 0xff;
  335. }
  336. ret = SDL_hid_write(device->dev, cmd, 7);
  337. if (ret == -1) {
  338. return false;
  339. }
  340. break;
  341. }
  342. case USB_DEVICE_ID_LOGITECH_WHEEL:
  343. // no range setting for ffex/dfex
  344. break;
  345. default:
  346. SDL_assert(0);
  347. }
  348. return true;
  349. }
  350. /*
  351. *Ported*
  352. Original functions by:
  353. Simon Wood <simon@mungewell.org>
  354. Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
  355. lg4ff_set_autocenter_default lg4ff_set_autocenter_ffex
  356. `git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
  357. */
  358. static bool HIDAPI_DriverLg4ff_SetAutoCenter(SDL_HIDAPI_Device *device, int magnitude)
  359. {
  360. SDL_DriverLg4ff_Context *ctx = (SDL_DriverLg4ff_Context *)device->context;
  361. Uint8 cmd[7] = {0};
  362. int ret;
  363. if (magnitude < 0) {
  364. magnitude = 0;
  365. }
  366. if (magnitude > 65535) {
  367. magnitude = 65535;
  368. }
  369. if (ctx->is_ffex) {
  370. magnitude = magnitude * 90 / 65535;
  371. cmd[0] = 0xfe;
  372. cmd[1] = 0x03;
  373. cmd[2] = (Uint8)((Uint16)magnitude >> 14);
  374. cmd[3] = (Uint8)((Uint16)magnitude >> 14);
  375. cmd[4] = (Uint8)magnitude;
  376. ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
  377. if(ret == -1){
  378. return false;
  379. }
  380. } else {
  381. Uint32 expand_a;
  382. Uint32 expand_b;
  383. // first disable
  384. cmd[0] = 0xf5;
  385. ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
  386. if (ret == -1) {
  387. return false;
  388. }
  389. if (magnitude == 0) {
  390. return true;
  391. }
  392. // set strength
  393. if (magnitude <= 0xaaaa) {
  394. expand_a = 0x0c * magnitude;
  395. expand_b = 0x80 * magnitude;
  396. } else {
  397. expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
  398. expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
  399. }
  400. // TODO do not adjust for MOMO wheels, when support is added
  401. expand_a = expand_a >> 1;
  402. SDL_memset(cmd, 0x00, sizeof(cmd));
  403. cmd[0] = 0xfe;
  404. cmd[1] = 0x0d;
  405. cmd[2] = (Uint8)(expand_a / 0xaaaa);
  406. cmd[3] = (Uint8)(expand_a / 0xaaaa);
  407. cmd[4] = (Uint8)(expand_b / 0xaaaa);
  408. ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
  409. if (ret == -1) {
  410. return false;
  411. }
  412. // enable
  413. SDL_memset(cmd, 0x00, sizeof(cmd));
  414. cmd[0] = 0x14;
  415. ret = SDL_hid_write(device->dev, cmd, sizeof(cmd));
  416. if (ret == -1) {
  417. return false;
  418. }
  419. }
  420. return true;
  421. }
  422. /*
  423. ffex identification method by:
  424. Simon Wood <simon@mungewell.org>
  425. Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
  426. lg4ff_init
  427. `git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
  428. */
  429. static bool HIDAPI_DriverLg4ff_InitDevice(SDL_HIDAPI_Device *device)
  430. {
  431. SDL_DriverLg4ff_Context *ctx;
  432. ctx = (SDL_DriverLg4ff_Context *)SDL_malloc(sizeof(SDL_DriverLg4ff_Context));
  433. if (ctx == NULL) {
  434. SDL_OutOfMemory();
  435. return false;
  436. }
  437. SDL_memset(ctx, 0, sizeof(SDL_DriverLg4ff_Context));
  438. device->context = ctx;
  439. device->joystick_type = SDL_JOYSTICK_TYPE_WHEEL;
  440. HIDAPI_SetDeviceName(device, HIDAPI_DriverLg4ff_GetDeviceName(device->product_id));
  441. if (SDL_hid_set_nonblocking(device->dev, 1) != 0) {
  442. return false;
  443. }
  444. if (!HIDAPI_DriverLg4ff_SetAutoCenter(device, 0)) {
  445. return false;
  446. }
  447. if (device->product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
  448. (device->version >> 8) == 0x21 &&
  449. (device->version & 0xff) == 0x00) {
  450. ctx->is_ffex = true;
  451. } else {
  452. ctx->is_ffex = false;
  453. }
  454. ctx->range = 900;
  455. return HIDAPI_JoystickConnected(device, NULL);
  456. }
  457. static int HIDAPI_DriverLg4ff_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  458. {
  459. return -1;
  460. }
  461. static void HIDAPI_DriverLg4ff_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  462. {
  463. }
  464. static bool HIDAPI_DriverLg4ff_GetBit(const Uint8 *buf, int bit_num, size_t buf_len)
  465. {
  466. int byte_offset = bit_num / 8;
  467. int local_bit = bit_num % 8;
  468. Uint8 mask = 1 << local_bit;
  469. if ((size_t)byte_offset >= buf_len) {
  470. SDL_assert(0);
  471. }
  472. return (buf[byte_offset] & mask) ? true : false;
  473. }
  474. /*
  475. *Ported*
  476. Original functions by:
  477. Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
  478. lg4ff_adjust_dfp_x_axis
  479. `git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
  480. */
  481. static Uint16 lg4ff_adjust_dfp_x_axis(Uint16 value, Uint16 range)
  482. {
  483. Uint16 max_range;
  484. Sint32 new_value;
  485. if (range == 900)
  486. return value;
  487. else if (range == 200)
  488. return value;
  489. else if (range < 200)
  490. max_range = 200;
  491. else
  492. max_range = 900;
  493. new_value = 8192 + ((value - 8192) * max_range / range);
  494. if (new_value < 0)
  495. return 0;
  496. else if (new_value > 16383)
  497. return 16383;
  498. else
  499. return (Uint16)new_value;
  500. }
  501. static bool HIDAPI_DriverLg4ff_HandleState(SDL_HIDAPI_Device *device,
  502. SDL_Joystick *joystick,
  503. Uint8 *report_buf,
  504. size_t report_size)
  505. {
  506. SDL_DriverLg4ff_Context *ctx = (SDL_DriverLg4ff_Context *)device->context;
  507. Uint8 hat = 0;
  508. Uint8 last_hat = 0;
  509. int num_buttons = HIDAPI_DriverLg4ff_GetNumberOfButtons(device->product_id);
  510. int bit_offset = 0;
  511. Uint64 timestamp = SDL_GetTicksNS();
  512. bool state_changed = false;
  513. switch (device->product_id) {
  514. case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
  515. case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
  516. case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
  517. case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
  518. hat = report_buf[0] & 0x0f;
  519. last_hat = ctx->last_report_buf[0] & 0x0f;
  520. break;
  521. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  522. hat = report_buf[3] >> 4;
  523. last_hat = ctx->last_report_buf[3] >> 4;
  524. break;
  525. case USB_DEVICE_ID_LOGITECH_WHEEL:
  526. hat = report_buf[2] & 0x0F;
  527. last_hat = ctx->last_report_buf[2] & 0x0F;
  528. break;
  529. default:
  530. SDL_assert(0);
  531. }
  532. if (hat != last_hat) {
  533. Uint8 sdl_hat = 0;
  534. state_changed = true;
  535. switch (hat) {
  536. case 0:
  537. sdl_hat = SDL_HAT_UP;
  538. break;
  539. case 1:
  540. sdl_hat = SDL_HAT_RIGHTUP;
  541. break;
  542. case 2:
  543. sdl_hat = SDL_HAT_RIGHT;
  544. break;
  545. case 3:
  546. sdl_hat = SDL_HAT_RIGHTDOWN;
  547. break;
  548. case 4:
  549. sdl_hat = SDL_HAT_DOWN;
  550. break;
  551. case 5:
  552. sdl_hat = SDL_HAT_LEFTDOWN;
  553. break;
  554. case 6:
  555. sdl_hat = SDL_HAT_LEFT;
  556. break;
  557. case 7:
  558. sdl_hat = SDL_HAT_LEFTUP;
  559. break;
  560. case 8:
  561. sdl_hat = SDL_HAT_CENTERED;
  562. break;
  563. // do not assert out, in case hardware can report weird hat values
  564. }
  565. SDL_SendJoystickHat(timestamp, joystick, 0, sdl_hat);
  566. }
  567. switch (device->product_id) {
  568. case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
  569. case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
  570. case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
  571. case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
  572. bit_offset = 4;
  573. break;
  574. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  575. bit_offset = 14;
  576. break;
  577. case USB_DEVICE_ID_LOGITECH_WHEEL:
  578. bit_offset = 0;
  579. break;
  580. default:
  581. SDL_assert(0);
  582. }
  583. if (device->product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
  584. // ref https://github.com/sonik-br/lgff_wheel_adapter/blob/d97f7823154818e1b3edff6d51498a122c302728/pico_lgff_wheel_adapter/reports.h#L265-L310
  585. // shifter_r is outside of the main button bit field for this particular wheel
  586. num_buttons--;
  587. bool button_on = HIDAPI_DriverLg4ff_GetBit(report_buf, 80, report_size);
  588. bool button_was_on = HIDAPI_DriverLg4ff_GetBit(ctx->last_report_buf, 80, report_size);
  589. if (button_on != button_was_on) {
  590. state_changed = true;
  591. SDL_SendJoystickButton(timestamp, joystick, (Uint8)(SDL_GAMEPAD_BUTTON_SOUTH + num_buttons), button_on);
  592. }
  593. }
  594. for (int i = 0;i < num_buttons;i++) {
  595. int bit_num = bit_offset + i;
  596. bool button_on = HIDAPI_DriverLg4ff_GetBit(report_buf, bit_num, report_size);
  597. bool button_was_on = HIDAPI_DriverLg4ff_GetBit(ctx->last_report_buf, bit_num, report_size);
  598. if(button_on != button_was_on){
  599. state_changed = true;
  600. SDL_SendJoystickButton(timestamp, joystick, (Uint8)(SDL_GAMEPAD_BUTTON_SOUTH + i), button_on);
  601. }
  602. }
  603. switch (device->product_id) {
  604. case USB_DEVICE_ID_LOGITECH_G29_WHEEL:{
  605. Uint16 x = *(Uint16 *)&report_buf[4];
  606. Uint16 last_x = *(Uint16 *)&ctx->last_report_buf[4];
  607. if (x != last_x) {
  608. state_changed = true;
  609. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, x - 32768);
  610. }
  611. if (report_buf[6] != ctx->last_report_buf[6]) {
  612. state_changed = true;
  613. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, report_buf[6] * 257 - 32768);
  614. }
  615. if (report_buf[7] != ctx->last_report_buf[7]) {
  616. state_changed = true;
  617. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, report_buf[7] * 257 - 32768);
  618. }
  619. if (report_buf[8] != ctx->last_report_buf[8]) {
  620. state_changed = true;
  621. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, report_buf[8] * 257 - 32768);
  622. }
  623. break;
  624. }
  625. case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
  626. case USB_DEVICE_ID_LOGITECH_G25_WHEEL:{
  627. Uint16 x = report_buf[4] << 6;
  628. Uint16 last_x = ctx->last_report_buf[4] << 6;
  629. x = x | report_buf[3] >> 2;
  630. last_x = last_x | ctx->last_report_buf[3] >> 2;
  631. if (x != last_x) {
  632. state_changed = true;
  633. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, x * 4 - 32768);
  634. }
  635. if (report_buf[5] != ctx->last_report_buf[5]) {
  636. state_changed = true;
  637. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, report_buf[5] * 257 - 32768);
  638. }
  639. if (report_buf[6] != ctx->last_report_buf[6]) {
  640. state_changed = true;
  641. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, report_buf[6] * 257 - 32768);
  642. }
  643. if (report_buf[7] != ctx->last_report_buf[7]) {
  644. state_changed = true;
  645. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, report_buf[7] * 257 - 32768);
  646. }
  647. break;
  648. }
  649. case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:{
  650. Uint16 x = report_buf[4];
  651. Uint16 last_x = ctx->last_report_buf[4];
  652. x = x | (report_buf[5] & 0x3F) << 8;
  653. last_x = last_x | (ctx->last_report_buf[5] & 0x3F) << 8;
  654. if (x != last_x) {
  655. state_changed = true;
  656. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, x * 4 - 32768);
  657. }
  658. if (report_buf[6] != ctx->last_report_buf[6]) {
  659. state_changed = true;
  660. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, report_buf[6] * 257 - 32768);
  661. }
  662. if (report_buf[7] != ctx->last_report_buf[7]) {
  663. state_changed = true;
  664. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, report_buf[7] * 257 - 32768);
  665. }
  666. break;
  667. }
  668. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:{
  669. Uint16 x = report_buf[0];
  670. Uint16 last_x = ctx->last_report_buf[0];
  671. x = x | (report_buf[1] & 0x3F) << 8;
  672. last_x = last_x | (ctx->last_report_buf[1] & 0x3F) << 8;
  673. if (x != last_x) {
  674. state_changed = true;
  675. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, lg4ff_adjust_dfp_x_axis(x, ctx->range) * 4 - 32768);
  676. }
  677. if (report_buf[5] != ctx->last_report_buf[5]) {
  678. state_changed = true;
  679. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, report_buf[5] * 257 - 32768);
  680. }
  681. if (report_buf[6] != ctx->last_report_buf[6]) {
  682. state_changed = true;
  683. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, report_buf[6] * 257 - 32768);
  684. }
  685. break;
  686. }
  687. case USB_DEVICE_ID_LOGITECH_WHEEL:{
  688. if (report_buf[3] != ctx->last_report_buf[3]) {
  689. state_changed = true;
  690. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, report_buf[3] * 257 - 32768);
  691. }
  692. if (report_buf[4] != ctx->last_report_buf[4]) {
  693. state_changed = true;
  694. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, report_buf[4] * 257 - 32768);
  695. }
  696. if (report_buf[5] != ctx->last_report_buf[5]) {
  697. state_changed = true;
  698. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, report_buf[5] * 257 - 32768);
  699. }
  700. if (report_buf[6] != ctx->last_report_buf[6]) {
  701. state_changed = true;
  702. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, report_buf[7] * 257 - 32768);
  703. }
  704. break;
  705. }
  706. default:
  707. SDL_assert(0);
  708. }
  709. SDL_memcpy(ctx->last_report_buf, report_buf, report_size);
  710. return state_changed;
  711. }
  712. static bool HIDAPI_DriverLg4ff_UpdateDevice(SDL_HIDAPI_Device *device)
  713. {
  714. SDL_Joystick *joystick = NULL;
  715. int r;
  716. Uint8 report_buf[32] = {0};
  717. size_t report_size = 0;
  718. SDL_DriverLg4ff_Context *ctx = (SDL_DriverLg4ff_Context *)device->context;
  719. if (device->num_joysticks > 0) {
  720. joystick = SDL_GetJoystickFromID(device->joysticks[0]);
  721. if (joystick == NULL) {
  722. return false;
  723. }
  724. } else {
  725. return false;
  726. }
  727. switch (device->product_id) {
  728. case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
  729. report_size = 12;
  730. break;
  731. case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
  732. case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
  733. report_size = 11;
  734. break;
  735. case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
  736. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  737. report_size = 8;
  738. break;
  739. case USB_DEVICE_ID_LOGITECH_WHEEL:
  740. report_size = 27;
  741. break;
  742. default:
  743. SDL_assert(0);
  744. }
  745. do {
  746. r = SDL_hid_read(device->dev, report_buf, report_size);
  747. if (r < 0) {
  748. /* Failed to read from controller */
  749. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  750. return false;
  751. } else if ((size_t)r == report_size) {
  752. bool state_changed = HIDAPI_DriverLg4ff_HandleState(device, joystick, report_buf, report_size);
  753. if(state_changed && !ctx->initialized) {
  754. ctx->initialized = true;
  755. HIDAPI_DriverLg4ff_SetRange(device, SDL_HIDAPI_DriverLg4ff_GetEnvInt("SDL_HIDAPI_LG4FF_RANGE", 40, 900, 900));
  756. HIDAPI_DriverLg4ff_SetAutoCenter(device, 0);
  757. }
  758. }
  759. } while (r > 0);
  760. return true;
  761. }
  762. static bool HIDAPI_DriverLg4ff_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  763. {
  764. SDL_AssertJoysticksLocked();
  765. // Initialize the joystick capabilities
  766. joystick->nhats = 1;
  767. joystick->nbuttons = HIDAPI_DriverLg4ff_GetNumberOfButtons(device->product_id);
  768. switch(device->product_id){
  769. case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
  770. case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
  771. case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
  772. case USB_DEVICE_ID_LOGITECH_WHEEL:
  773. joystick->naxes = 4;
  774. break;
  775. case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
  776. joystick->naxes = 3;
  777. break;
  778. case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
  779. joystick->naxes = 3;
  780. break;
  781. default:
  782. SDL_assert(0);
  783. }
  784. return true;
  785. }
  786. static bool HIDAPI_DriverLg4ff_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  787. {
  788. return SDL_Unsupported();
  789. }
  790. static bool HIDAPI_DriverLg4ff_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  791. {
  792. return SDL_Unsupported();
  793. }
  794. static Uint32 HIDAPI_DriverLg4ff_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  795. {
  796. switch(device->product_id) {
  797. case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
  798. case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
  799. return SDL_JOYSTICK_CAP_MONO_LED;
  800. default:
  801. return 0;
  802. }
  803. }
  804. /*
  805. Commands by:
  806. Michal Malý <madcatxster@devoid-pointer.net> <madcatxster@gmail.com>
  807. Simon Wood <simon@mungewell.org>
  808. lg4ff_led_set_brightness lg4ff_set_leds
  809. `git blame v6.12 drivers/hid/hid-lg4ff.c`, https://github.com/torvalds/linux.git
  810. */
  811. static bool HIDAPI_DriverLg4ff_SendLedCommand(SDL_HIDAPI_Device *device, Uint8 state)
  812. {
  813. Uint8 cmd[7];
  814. Uint8 led_state = 0;
  815. switch (state) {
  816. case 0:
  817. led_state = 0;
  818. break;
  819. case 1:
  820. led_state = 1;
  821. break;
  822. case 2:
  823. led_state = 3;
  824. break;
  825. case 3:
  826. led_state = 7;
  827. break;
  828. case 4:
  829. led_state = 15;
  830. break;
  831. case 5:
  832. led_state = 31;
  833. break;
  834. default:
  835. SDL_assert(0);
  836. }
  837. cmd[0] = 0xf8;
  838. cmd[1] = 0x12;
  839. cmd[2] = led_state;
  840. cmd[3] = 0x00;
  841. cmd[4] = 0x00;
  842. cmd[5] = 0x00;
  843. cmd[6] = 0x00;
  844. return SDL_hid_write(device->dev, cmd, sizeof(cmd)) == sizeof(cmd);
  845. }
  846. static bool HIDAPI_DriverLg4ff_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  847. {
  848. int max_led = red;
  849. // only g27/g29, and g923 when supported is added
  850. if (device->product_id != USB_DEVICE_ID_LOGITECH_G29_WHEEL &&
  851. device->product_id != USB_DEVICE_ID_LOGITECH_G27_WHEEL) {
  852. return SDL_Unsupported();
  853. }
  854. if (green > max_led) {
  855. max_led = green;
  856. }
  857. if (blue > max_led) {
  858. max_led = blue;
  859. }
  860. return HIDAPI_DriverLg4ff_SendLedCommand(device, (Uint8)((5 * max_led) / 255));
  861. }
  862. static bool HIDAPI_DriverLg4ff_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
  863. {
  864. // allow programs to send raw commands
  865. return SDL_hid_write(device->dev, data, size) == size;
  866. }
  867. static bool HIDAPI_DriverLg4ff_SetSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
  868. {
  869. // On steam deck, sensors are enabled by default. Nothing to do here.
  870. return SDL_Unsupported();
  871. }
  872. static void HIDAPI_DriverLg4ff_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  873. {
  874. // remember to stop effects on haptics close, when implemented
  875. HIDAPI_DriverLg4ff_SetJoystickLED(device, joystick, 0, 0, 0);
  876. }
  877. static void HIDAPI_DriverLg4ff_FreeDevice(SDL_HIDAPI_Device *device)
  878. {
  879. // device context is freed in SDL_hidapijoystick.c
  880. }
  881. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverLg4ff = {
  882. SDL_HINT_JOYSTICK_HIDAPI_LG4FF,
  883. true,
  884. HIDAPI_DriverLg4ff_RegisterHints,
  885. HIDAPI_DriverLg4ff_UnregisterHints,
  886. HIDAPI_DriverLg4ff_IsEnabled,
  887. HIDAPI_DriverLg4ff_IsSupportedDevice,
  888. HIDAPI_DriverLg4ff_InitDevice,
  889. HIDAPI_DriverLg4ff_GetDevicePlayerIndex,
  890. HIDAPI_DriverLg4ff_SetDevicePlayerIndex,
  891. HIDAPI_DriverLg4ff_UpdateDevice,
  892. HIDAPI_DriverLg4ff_OpenJoystick,
  893. HIDAPI_DriverLg4ff_RumbleJoystick,
  894. HIDAPI_DriverLg4ff_RumbleJoystickTriggers,
  895. HIDAPI_DriverLg4ff_GetJoystickCapabilities,
  896. HIDAPI_DriverLg4ff_SetJoystickLED,
  897. HIDAPI_DriverLg4ff_SendJoystickEffect,
  898. HIDAPI_DriverLg4ff_SetSensorsEnabled,
  899. HIDAPI_DriverLg4ff_CloseJoystick,
  900. HIDAPI_DriverLg4ff_FreeDevice,
  901. };
  902. #endif /* SDL_JOYSTICK_HIDAPI_LG4FF */
  903. #endif /* SDL_JOYSTICK_HIDAPI */