HIDGetButtonsOnPage.c [plain text]
#include "HIDLib.h"
OSStatus HIDGetButtonsOnPage(HIDReportType reportType,
HIDUsage usagePage,
UInt32 iCollection,
HIDUsage *piUsageList,
UInt32 *piUsageListLength,
HIDPreparsedDataRef preparsedDataRef,
void *psReport,
UInt32 iReportLength)
{
HIDPreparsedDataPtr ptPreparsedData = (HIDPreparsedDataPtr) preparsedDataRef;
HIDUsageAndPage tUsageAndPage;
HIDCollection *ptCollection;
HIDReportItem *ptReportItem;
OSStatus iStatus;
int iR, iE;
long iValue;
int iStart;
int iMaxUsages;
int iReportItem;
Boolean bIncompatibleReport = false;
Boolean butNotReally = false;
if ((ptPreparsedData == NULL)
|| (piUsageList == NULL)
|| (piUsageListLength == NULL)
|| (psReport == NULL))
return kHIDNullPointerErr;
if (ptPreparsedData->hidTypeIfValid != kHIDOSType)
return kHIDInvalidPreparsedDataErr;
if ((iCollection < 0) || (iCollection >= ptPreparsedData->collectionCount))
return kHIDBadParameterErr;
iMaxUsages = *piUsageListLength;
*piUsageListLength = 0;
ptCollection = &ptPreparsedData->collections[iCollection];
for (iR=0; iR<ptCollection->reportItemCount; iR++)
{
iReportItem = ptCollection->firstReportItem + iR;
ptReportItem = &ptPreparsedData->reportItems[iReportItem];
if (HIDIsButton(ptReportItem, preparsedDataRef))
{
iStatus = HIDCheckReport(reportType,preparsedDataRef,ptReportItem,
psReport,iReportLength);
if (iStatus == kHIDIncompatibleReportErr)
bIncompatibleReport = true;
else if (iStatus != kHIDSuccess)
return iStatus;
else
{
butNotReally = true;
iStart = ptReportItem->startBit;
for (iE=0; iE<ptReportItem->globals.reportCount; iE++)
{
if ((ptReportItem->dataModes & kHIDDataArrayBit) == kHIDDataArray)
{
iStatus = HIDGetData(psReport, iReportLength, iStart,
ptReportItem->globals.reportSize,
&iValue, false);
if (!iStatus)
iStatus = HIDPostProcessRIValue (ptReportItem, &iValue);
HIDUsageAndPageFromIndex(preparsedDataRef,
ptReportItem,
iValue-ptReportItem->globals.logicalMinimum,
&tUsageAndPage);
iStart += ptReportItem->globals.reportSize;
if (usagePage == tUsageAndPage.usagePage)
{
if (*piUsageListLength >= iMaxUsages)
return kHIDBufferTooSmallErr;
piUsageList[(*piUsageListLength)++] = iValue;
}
}
else
{
iStatus = HIDGetData(psReport, iReportLength, iStart, 1, &iValue, false);
if (!iStatus)
iStatus = HIDPostProcessRIValue (ptReportItem, &iValue);
iStart++;
if (!iStatus && iValue != 0)
{
HIDUsageAndPageFromIndex(preparsedDataRef,ptReportItem,iE,&tUsageAndPage);
if (usagePage == tUsageAndPage.usagePage)
{
if (*piUsageListLength >= iMaxUsages)
return kHIDBufferTooSmallErr;
piUsageList[(*piUsageListLength)++] = tUsageAndPage.usage;
}
}
}
}
}
}
}
if (*piUsageListLength == 0)
{
if (bIncompatibleReport && !butNotReally)
return kHIDIncompatibleReportErr;
return kHIDUsageNotFoundErr;
}
return kHIDSuccess;
}