123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
-
-
-
-
-
-
-
-
- #import <IOKit/IOKitLib.h>
-
- #import "fooHID.h"
-
- #define CHANNELS 6
- #define CHANNELMAXIMUM 1022
- #define CHANNELOFFSET (CHANNELMAXIMUM / 2)
-
- #define FOOHID_NAME "it_unbit_foohid"
- #define FOOHID_CREATE 0
- #define FOOHID_DESTROY 1
- #define FOOHID_SEND 2
- #define FOOHID_LIST 3
- #define VIRTUAL_DEVICE_NAME "Virtual Serial Transmitter"
-
- int foohidInit();
- void foohidClose();
- void foohidSend(uint16_t *data);
-
- @implementation fooHID
-
- + (NSInteger)init {
- return foohidInit();
- }
-
- + (void)close {
- return foohidClose();
- }
-
- + (void)send:(NSArray *)data {
- if ([data count] < CHANNELS) {
- NSLog(@"Not enough data values to send (%lu)!\n", (unsigned long)[data count]);
- } else {
- uint16_t buffer[CHANNELS];
- for (int i = 0; i < CHANNELS; i++) {
- buffer[i] = [((NSNumber *)[data objectAtIndex:i]) integerValue];
- }
- foohidSend(buffer);
- }
- }
-
- @end
-
- struct gamepad_report_t {
- int16_t leftX;
- int16_t leftY;
- int16_t rightX;
- int16_t rightY;
- int16_t aux1;
- int16_t aux2;
- };
-
- static io_connect_t connector;
- static uint64_t input[4];
- static struct gamepad_report_t gamepad;
-
-
- static char report_descriptor[36] = {
- 0x05, 0x01,
- 0x09, 0x05,
- 0xa1, 0x01,
- 0xa1, 0x00,
- 0x05, 0x01,
- 0x09, 0x30,
- 0x09, 0x31,
- 0x09, 0x32,
- 0x09, 0x33,
- 0x09, 0x34,
- 0x09, 0x35,
- 0x16, 0x01, 0xfe,
- 0x26, 0xff, 0x01,
- 0x75, 0x10,
- 0x95, 0x06,
- 0x81, 0x02,
- 0xc0,
- 0xc0
- };
-
- int foohidInit() {
- NSLog(@"Searching for foohid Kernel extension...\n");
-
-
- io_iterator_t iterator;
- kern_return_t ret = IOServiceGetMatchingServices(kIOMasterPortDefault,
- IOServiceMatching(FOOHID_NAME), &iterator);
- if (ret != KERN_SUCCESS) {
- NSLog(@"Unable to access foohid IOService\n");
- return 1;
- }
-
- int found = 0;
- io_service_t service;
- while ((service = IOIteratorNext(iterator)) != IO_OBJECT_NULL) {
- ret = IOServiceOpen(service, mach_task_self(), 0, &connector);
- if (ret == KERN_SUCCESS) {
- found = 1;
- break;
- }
- }
- IOObjectRelease(iterator);
- if (!found) {
- NSLog(@"Unable to open foohid IOService\n");
- return 1;
- }
-
- NSLog(@"Creating virtual HID device...\n");
-
- input[0] = (uint64_t)strdup(VIRTUAL_DEVICE_NAME);
- input[1] = strlen((char*)input[0]);
-
- input[2] = (uint64_t)report_descriptor;
- input[3] = sizeof(report_descriptor);
-
- uint32_t output_count = 1;
- uint64_t output = 0;
- ret = IOConnectCallScalarMethod(connector, FOOHID_CREATE, input, 4, &output, &output_count);
- if (ret != KERN_SUCCESS) {
- NSLog(@"Unable to create virtual HID device\n");
- return 1;
- }
-
- return 0;
- }
-
- void foohidClose() {
- NSLog(@"Destroying virtual HID device\n");
-
- uint32_t output_count = 1;
- uint64_t output = 0;
- kern_return_t ret = IOConnectCallScalarMethod(connector, FOOHID_DESTROY, input, 2, &output, &output_count);
- if (ret != KERN_SUCCESS) {
- NSLog(@"Unable to destroy virtual HID device\n");
- }
- }
-
- void foohidSend(uint16_t *data) {
- for (int i = 0; i < CHANNELS; i++) {
- if (data[i] > CHANNELMAXIMUM) {
- data[i] = CHANNELMAXIMUM;
- }
- }
-
- gamepad.leftX = data[3] - CHANNELOFFSET;
- gamepad.leftY = data[2] - CHANNELOFFSET;
- gamepad.rightX = data[0] - CHANNELOFFSET;
- gamepad.rightY = data[1] - CHANNELOFFSET;
- gamepad.aux1 = data[4] - CHANNELOFFSET;
- gamepad.aux2 = data[5] - CHANNELOFFSET;
-
-
-
-
- input[2] = (uint64_t)&gamepad;
- input[3] = sizeof(struct gamepad_report_t);
-
- uint32_t output_count = 1;
- uint64_t output = 0;
- kern_return_t ret = IOConnectCallScalarMethod(connector, FOOHID_SEND, input, 4, &output, &output_count);
- if (ret != KERN_SUCCESS) {
- NSLog(@"Unable to send packet to virtual HID device\n");
- }
- }
|