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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 deviceName = 0, deviceNameLength;
  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. if (deviceName == 0) {
  104. deviceName = (uint64_t)strdup(VIRTUAL_DEVICE_NAME);
  105. deviceNameLength = strlen((char *)deviceName);
  106. }
  107. uint64_t input[4];
  108. input[0] = deviceName;
  109. input[1] = deviceNameLength;
  110. input[2] = (uint64_t)report_descriptor;
  111. input[3] = sizeof(report_descriptor);
  112. uint32_t output_count = 1;
  113. uint64_t output = 0;
  114. ret = IOConnectCallScalarMethod(connector, FOOHID_CREATE, input, 4, &output, &output_count);
  115. if (ret != KERN_SUCCESS) {
  116. NSLog(@"Unable to create virtual HID device\n");
  117. return 1;
  118. }
  119. return 0;
  120. }
  121. void foohidClose() {
  122. NSLog(@"Destroying virtual HID device\n");
  123. uint64_t input[2];
  124. input[0] = deviceName;
  125. input[1] = deviceNameLength;
  126. uint32_t output_count = 1;
  127. uint64_t output = 0;
  128. kern_return_t ret = IOConnectCallScalarMethod(connector, FOOHID_DESTROY, input, 2, &output, &output_count);
  129. if (ret != KERN_SUCCESS) {
  130. NSLog(@"Unable to destroy virtual HID device\n");
  131. }
  132. }
  133. void foohidSend(uint16_t *data) {
  134. for (int i = 0; i < CHANNELS; i++) {
  135. if (data[i] > CHANNELMAXIMUM) {
  136. data[i] = CHANNELMAXIMUM;
  137. }
  138. }
  139. gamepad.leftX = data[3] - CHANNELOFFSET;
  140. gamepad.leftY = data[2] - CHANNELOFFSET;
  141. gamepad.rightX = data[0] - CHANNELOFFSET;
  142. gamepad.rightY = data[1] - CHANNELOFFSET;
  143. gamepad.aux1 = data[4] - CHANNELOFFSET;
  144. gamepad.aux2 = data[5] - CHANNELOFFSET;
  145. /*
  146. NSLog(@"Sending data packet:\n");
  147. NSLog(@"Left X: %d\n", gamepad.leftX);
  148. NSLog(@"Left Y: %d\n", gamepad.leftY);
  149. NSLog(@"Right X: %d\n", gamepad.rightX);
  150. NSLog(@"Right Y: %d\n", gamepad.rightY);
  151. NSLog(@"Aux 1: %d\n", gamepad.aux1);
  152. NSLog(@"Aux 2: %d\n", gamepad.aux2);
  153. */
  154. uint64_t input[4];
  155. input[0] = deviceName;
  156. input[1] = deviceNameLength;
  157. input[2] = (uint64_t)&gamepad;
  158. input[3] = sizeof(struct gamepad_report_t);
  159. uint32_t output_count = 1;
  160. uint64_t output = 0;
  161. kern_return_t ret = IOConnectCallScalarMethod(connector, FOOHID_SEND, input, 4, &output, &output_count);
  162. if (ret != KERN_SUCCESS) {
  163. NSLog(@"Unable to send packet to virtual HID device\n");
  164. }
  165. }