dfmoore
2006.06.26, 02:18 AM
Hey all, I decided to take a look at HID utilities and get something running with it. I wanted to post my notes to see if anyone had comments about things I might be doing that are foolish. Maybe someone else is looking into this and possibly this could help jump start?
First, I got the Utilities files from apple's developer page:
http://developer.apple.com/samplecode/HID_Utilities_Source/index.html
I put the files from that folder into my project folder (organize as you wish) and brought in one header file, "HID_Utilities_External.h" into the xCode project.
Next, I brought in "libHIDUtilities.dylib" (also from the HID utilities folder) and added a step in my target to copy the framework into the final executable. I have that step before the others.
In my main.cpp, I added:
#include <IOKit/HID/IOHIDKeys.h>
#include "HID_Utilities_External.h"
Finally, here is some step by step code that helped me figure out how to access data from external devices. It helps to have the "HID explorer" project from apple's developer section to look at what device (mouse, keyboard, joystick, etc) is first, second, etc and what the element (right button, tracking on the X axis...) order is.
void HID_setup()
{
printf("\n");
//HID 1. Set up the Device List, putting them into the usage pages:
unsigned long usagePage;
unsigned long usage;
unsigned char build_return;
build_return=HIDBuildDeviceList (usagePage, usage);
printf("Return from HID Build Device List: %d\n", build_return);
//HID 2. Check and make sure the list has been made:
unsigned char have_list_return;
have_list_return=HIDHaveDeviceList();
printf("Return from HID Have Device List: %d\n", have_list_return);
//HID 3. How many devices did we find?
unsigned long hid_count;
hid_count=HIDCountDevices();
printf("HID manager found %d devices.\n",hid_count);
//HID 4. Let's get the first device:
pRecDevice hid_one;
hid_one=HIDGetFirstDevice ();
if(hid_one==NULL) printf("First Device failed to be retrieved.\n");
//HID 5. How many elements are in this device?
unsigned long hid_element_count;
hid_element_count=HIDCountDeviceElements (hid_one, kHIDElementTypeIO); //hid_one is from step 4.
printf("First item has %d elements.\n",hid_element_count);
//HID 6. Catpure the first element
pRecElement hid_one_one;
hid_one_one=HIDGetFirstDeviceElement (hid_one, kHIDElementTypeIO);
//HID 7. Queue the first element up for polling:
unsigned long data;
data=HIDQueueElement (hid_one, hid_one_one);
printf("HIDQueueElement returns: %d. Not that I know what this means.\n",data);
//HID 8. ...Or, we can go ahead and add all elements for polling.
//Note: even if we queued up an element, this will take care & clean up previous elements.
data= HIDQueueDevice (hid_one);
printf("More mystical return numbers from HIDQueueDevice: %d.\n",data);
//HID 9. Time to actually get information!!
//long HIDGetElementValue (pRecDevice pDevice, pRecElement pElement);
long HID_data= HIDGetElementValue (hid_one, hid_one_one);
printf("one reports: %d\n",HID_data);
//HID end:
HIDReleaseDeviceList();
printf("HID Device List Released.");
}
Some things to point out: This is NOT clean code. It was something I took an hour to play with and get running and reporting information. Two, I'm using printf because it's just second nature. I started with C on embedded microprocessors and I just didn't use cin/cout though I should have. I just realized that.
Finally, the power of something like this will be realized with well conceived data structures and some loops to capture element data. This is just to get one element's info. The readme that comes with the HID utilities is great and doesn't take long to figure out.
Sorry to those of you who are much better at this than I am, and had to suffer through this! :)
One big question to ask though: The dynamic library I'm linking seems to be PPC. If I want to eventually bring this over to universal binaries, how much difficulty will I have with just this functionality? :???:
First, I got the Utilities files from apple's developer page:
http://developer.apple.com/samplecode/HID_Utilities_Source/index.html
I put the files from that folder into my project folder (organize as you wish) and brought in one header file, "HID_Utilities_External.h" into the xCode project.
Next, I brought in "libHIDUtilities.dylib" (also from the HID utilities folder) and added a step in my target to copy the framework into the final executable. I have that step before the others.
In my main.cpp, I added:
#include <IOKit/HID/IOHIDKeys.h>
#include "HID_Utilities_External.h"
Finally, here is some step by step code that helped me figure out how to access data from external devices. It helps to have the "HID explorer" project from apple's developer section to look at what device (mouse, keyboard, joystick, etc) is first, second, etc and what the element (right button, tracking on the X axis...) order is.
void HID_setup()
{
printf("\n");
//HID 1. Set up the Device List, putting them into the usage pages:
unsigned long usagePage;
unsigned long usage;
unsigned char build_return;
build_return=HIDBuildDeviceList (usagePage, usage);
printf("Return from HID Build Device List: %d\n", build_return);
//HID 2. Check and make sure the list has been made:
unsigned char have_list_return;
have_list_return=HIDHaveDeviceList();
printf("Return from HID Have Device List: %d\n", have_list_return);
//HID 3. How many devices did we find?
unsigned long hid_count;
hid_count=HIDCountDevices();
printf("HID manager found %d devices.\n",hid_count);
//HID 4. Let's get the first device:
pRecDevice hid_one;
hid_one=HIDGetFirstDevice ();
if(hid_one==NULL) printf("First Device failed to be retrieved.\n");
//HID 5. How many elements are in this device?
unsigned long hid_element_count;
hid_element_count=HIDCountDeviceElements (hid_one, kHIDElementTypeIO); //hid_one is from step 4.
printf("First item has %d elements.\n",hid_element_count);
//HID 6. Catpure the first element
pRecElement hid_one_one;
hid_one_one=HIDGetFirstDeviceElement (hid_one, kHIDElementTypeIO);
//HID 7. Queue the first element up for polling:
unsigned long data;
data=HIDQueueElement (hid_one, hid_one_one);
printf("HIDQueueElement returns: %d. Not that I know what this means.\n",data);
//HID 8. ...Or, we can go ahead and add all elements for polling.
//Note: even if we queued up an element, this will take care & clean up previous elements.
data= HIDQueueDevice (hid_one);
printf("More mystical return numbers from HIDQueueDevice: %d.\n",data);
//HID 9. Time to actually get information!!
//long HIDGetElementValue (pRecDevice pDevice, pRecElement pElement);
long HID_data= HIDGetElementValue (hid_one, hid_one_one);
printf("one reports: %d\n",HID_data);
//HID end:
HIDReleaseDeviceList();
printf("HID Device List Released.");
}
Some things to point out: This is NOT clean code. It was something I took an hour to play with and get running and reporting information. Two, I'm using printf because it's just second nature. I started with C on embedded microprocessors and I just didn't use cin/cout though I should have. I just realized that.
Finally, the power of something like this will be realized with well conceived data structures and some loops to capture element data. This is just to get one element's info. The readme that comes with the HID utilities is great and doesn't take long to figure out.
Sorry to those of you who are much better at this than I am, and had to suffer through this! :)
One big question to ask though: The dynamic library I'm linking seems to be PPC. If I want to eventually bring this over to universal binaries, how much difficulty will I have with just this functionality? :???: