Mac OS X gamepad emulator for serial RC transmitters
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fooHID.m 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // fooHID.m
  3. // SerialGamepad
  4. //
  5. // Created by Thomas Buck on 14.12.15.
  6. // Copyright © 2015 xythobuz. All rights reserved.
  7. //
  8. #import <IOKit/IOKitLib.h>
  9. #import "fooHID.h"
  10. #define CHANNELS 6
  11. #define CHANNELMAXIMUM 1022
  12. #define CHANNELOFFSET (CHANNELMAXIMUM / 2)
  13. #define FOOHID_NAME "it_unbit_foohid"
  14. #define FOOHID_CREATE 0
  15. #define FOOHID_DESTROY 1
  16. #define FOOHID_SEND 2
  17. #define FOOHID_LIST 3
  18. #define VIRTUAL_DEVICE_NAME "Virtual Serial Transmitter"
  19. int foohidInit();
  20. void foohidClose();
  21. void foohidSend(uint16_t *data);
  22. @implementation fooHID
  23. + (NSInteger)init {
  24. return foohidInit();
  25. }
  26. + (void)close {
  27. return foohidClose();
  28. }
  29. + (void)send:(NSArray *)data {
  30. if ([data count] < CHANNELS) {
  31. NSLog(@"Not enough data values to send (%lu)!\n", (unsigned long)[data count]);
  32. } else {
  33. uint16_t buffer[CHANNELS];
  34. for (int i = 0; i < CHANNELS; i++) {
  35. buffer[i] = [((NSNumber *)[data objectAtIndex:i]) integerValue];
  36. }
  37. foohidSend(buffer);
  38. }
  39. }
  40. @end
  41. struct gamepad_report_t {
  42. int16_t leftX;
  43. int16_t leftY;
  44. int16_t rightX;
  45. int16_t rightY;
  46. int16_t aux1;
  47. int16_t aux2;
  48. };
  49. static io_connect_t connector;
  50. static uint64_t input[4];
  51. static struct gamepad_report_t gamepad;
  52. /*
  53. * This is my USB HID Descriptor for this emulated Gamepad.
  54. * For more informations refer to:
  55. * http://eleccelerator.com/tutorial-about-usb-hid-report-descriptors/
  56. * http://www.usb.org/developers/hidpage#HID%20Descriptor%20Tool
  57. */
  58. static char report_descriptor[36] = {
  59. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  60. 0x09, 0x05, // USAGE (Game Pad)
  61. 0xa1, 0x01, // COLLECTION (Application)
  62. 0xa1, 0x00, // COLLECTION (Physical)
  63. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  64. 0x09, 0x30, // USAGE (X)
  65. 0x09, 0x31, // USAGE (Y)
  66. 0x09, 0x32, // USAGE (Z)
  67. 0x09, 0x33, // USAGE (Rx)
  68. 0x09, 0x34, // USAGE (Ry)
  69. 0x09, 0x35, // USAGE (Rz)
  70. 0x16, 0x01, 0xfe, // LOGICAL_MINIMUM (-511)
  71. 0x26, 0xff, 0x01, // LOGICAL_MAXIMUM (511)
  72. 0x75, 0x10, // REPORT_SIZE (16)
  73. 0x95, 0x06, // REPORT_COUNT (6)
  74. 0x81, 0x02, // INPUT (Data,Var,Abs)
  75. 0xc0, // END_COLLECTION
  76. 0xc0 // END_COLLECTION
  77. };
  78. int foohidInit() {
  79. NSLog(@"Searching for foohid Kernel extension...\n");
  80. // get a reference to the IOService
  81. io_iterator_t iterator;
  82. kern_return_t ret = IOServiceGetMatchingServices(kIOMasterPortDefault,
  83. IOServiceMatching(FOOHID_NAME), &iterator);
  84. if (ret != KERN_SUCCESS) {
  85. NSLog(@"Unable to access foohid IOService\n");
  86. return 1;
  87. }
  88. int found = 0;
  89. io_service_t service;
  90. while ((service = IOIteratorNext(iterator)) != IO_OBJECT_NULL) {
  91. ret = IOServiceOpen(service, mach_task_self(), 0, &connector);
  92. if (ret == KERN_SUCCESS) {
  93. found = 1;
  94. break;
  95. }
  96. }
  97. IOObjectRelease(iterator);
  98. if (!found) {
  99. NSLog(@"Unable to open foohid IOService\n");
  100. return 1;
  101. }
  102. NSLog(@"Creating virtual HID device...\n");
  103. input[0] = (uint64_t)strdup(VIRTUAL_DEVICE_NAME);
  104. input[1] = strlen((char*)input[0]);
  105. input[2] = (uint64_t)report_descriptor;
  106. input[3] = sizeof(report_descriptor);
  107. uint32_t output_count = 1;
  108. uint64_t output = 0;
  109. ret = IOConnectCallScalarMethod(connector, FOOHID_CREATE, input, 4, &output, &output_count);
  110. if (ret != KERN_SUCCESS) {
  111. NSLog(@"Unable to create virtual HID device\n");
  112. return 1;
  113. }
  114. return 0;
  115. }
  116. void foohidClose() {
  117. NSLog(@"Destroying virtual HID device\n");
  118. uint32_t output_count = 1;
  119. uint64_t output = 0;
  120. kern_return_t ret = IOConnectCallScalarMethod(connector, FOOHID_DESTROY, input, 2, &output, &output_count);
  121. if (ret != KERN_SUCCESS) {
  122. NSLog(@"Unable to destroy virtual HID device\n");
  123. }
  124. }
  125. void foohidSend(uint16_t *data) {
  126. for (int i = 0; i < CHANNELS; i++) {
  127. if (data[i] > CHANNELMAXIMUM) {
  128. data[i] = CHANNELMAXIMUM;
  129. }
  130. }
  131. gamepad.leftX = data[3] - CHANNELOFFSET;
  132. gamepad.leftY = data[2] - CHANNELOFFSET;
  133. gamepad.rightX = data[0] - CHANNELOFFSET;
  134. gamepad.rightY = data[1] - CHANNELOFFSET;
  135. gamepad.aux1 = data[4] - CHANNELOFFSET;
  136. gamepad.aux2 = data[5] - CHANNELOFFSET;
  137. /*
  138. NSLog(@"Sending data packet:\n");
  139. NSLog(@"Left X: %d\n", gamepad.leftX);
  140. NSLog(@"Left Y: %d\n", gamepad.leftY);
  141. NSLog(@"Right X: %d\n", gamepad.rightX);
  142. NSLog(@"Right Y: %d\n", gamepad.rightY);
  143. NSLog(@"Aux 1: %d\n", gamepad.aux1);
  144. NSLog(@"Aux 2: %d\n", gamepad.aux2);
  145. */
  146. input[2] = (uint64_t)&gamepad;
  147. input[3] = sizeof(struct gamepad_report_t);
  148. uint32_t output_count = 1;
  149. uint64_t output = 0;
  150. kern_return_t ret = IOConnectCallScalarMethod(connector, FOOHID_SEND, input, 4, &output, &output_count);
  151. if (ret != KERN_SUCCESS) {
  152. NSLog(@"Unable to send packet to virtual HID device\n");
  153. }
  154. }