HIDGetUsageValue.c [plain text]
#include "HIDLib.h"
OSStatus HIDGetUsageValue
(HIDReportType reportType,
HIDUsage usagePage,
UInt32 iCollection,
HIDUsage usage,
SInt32 * piUsageValue,
HIDPreparsedDataRef preparsedDataRef,
void * psReport,
ByteCount iReportLength)
{
HIDPreparsedDataPtr ptPreparsedData = (HIDPreparsedDataPtr) preparsedDataRef;
HIDCollection *ptCollection;
HIDReportItem *ptReportItem;
OSStatus iStatus;
int iR;
SInt32 iValue;
int iStart;
int iReportItem;
UInt32 iUsageIndex;
Boolean bIncompatibleReport = false;
Boolean butNotReally = false;
if ((ptPreparsedData == NULL)
|| (piUsageValue == NULL)
|| (psReport == NULL))
return kHIDNullPointerErr;
if (ptPreparsedData->hidTypeIfValid != kHIDOSType)
return kHIDInvalidPreparsedDataErr;
if ((iCollection < 0) || (iCollection >= ptPreparsedData->collectionCount))
return kHIDBadParameterErr;
ptCollection = &ptPreparsedData->collections[iCollection];
for (iR=0; iR<ptCollection->reportItemCount; iR++)
{
iReportItem = ptCollection->firstReportItem + iR;
ptReportItem = &ptPreparsedData->reportItems[iReportItem];
if (HIDIsVariable(ptReportItem, preparsedDataRef)
&& HIDHasUsage(preparsedDataRef,ptReportItem,usagePage,usage,&iUsageIndex,NULL))
{
iStatus = HIDCheckReport(reportType,preparsedDataRef,ptReportItem,
psReport,iReportLength);
if (iStatus == kHIDIncompatibleReportErr)
bIncompatibleReport = true;
else if (iStatus != kHIDSuccess)
return iStatus;
else
{
butNotReally = true;
iStart = ptReportItem->startBit
+ (ptReportItem->globals.reportSize * iUsageIndex);
iStatus = HIDGetData(psReport, iReportLength, iStart,
ptReportItem->globals.reportSize, &iValue,
((ptReportItem->globals.logicalMinimum < 0)
||(ptReportItem->globals.logicalMaximum < 0)));
if (!iStatus)
iStatus = HIDPostProcessRIValue (ptReportItem, &iValue);
*piUsageValue = iValue;
return iStatus;
}
}
}
if (bIncompatibleReport && !butNotReally)
return kHIDIncompatibleReportErr;
return kHIDUsageNotFoundErr;
}
OSStatus HIDGetScaledUsageValue(HIDReportType reportType,
HIDUsage usagePage,
UInt32 iCollection,
HIDUsage usage,
SInt32 *piUsageValue,
HIDPreparsedDataRef preparsedDataRef,
void *psReport,
ByteCount iReportLength)
{
HIDPreparsedDataPtr ptPreparsedData = (HIDPreparsedDataPtr) preparsedDataRef;
HIDCollection *ptCollection;
HIDReportItem *ptReportItem;
OSStatus iStatus;
int iR;
long iValue;
int iStart;
int iReportItem;
UInt32 iUsageIndex;
Boolean bIncompatibleReport = false;
Boolean butNotReally = false;
if ((ptPreparsedData == NULL)
|| (piUsageValue == NULL)
|| (psReport == NULL))
return kHIDNullPointerErr;
if (ptPreparsedData->hidTypeIfValid != kHIDOSType)
return kHIDInvalidPreparsedDataErr;
if ((iCollection < 0) || (iCollection >= ptPreparsedData->collectionCount))
return kHIDBadParameterErr;
ptCollection = &ptPreparsedData->collections[iCollection];
for (iR=0; iR<ptCollection->reportItemCount; iR++)
{
iReportItem = ptCollection->firstReportItem + iR;
ptReportItem = &ptPreparsedData->reportItems[iReportItem];
if (HIDIsVariable(ptReportItem, preparsedDataRef)
&& HIDHasUsage(preparsedDataRef,ptReportItem,usagePage,usage,&iUsageIndex,NULL))
{
iStatus = HIDCheckReport(reportType,preparsedDataRef,ptReportItem,
psReport,iReportLength);
if (iStatus == kHIDIncompatibleReportErr)
bIncompatibleReport = true;
else if (iStatus != kHIDSuccess)
return iStatus;
else
{
butNotReally = true;
iStart = ptReportItem->startBit
+ (ptReportItem->globals.reportSize * iUsageIndex);
iStatus = HIDGetData(psReport, iReportLength, iStart,
ptReportItem->globals.reportSize, &iValue,
((ptReportItem->globals.logicalMinimum < 0)
||(ptReportItem->globals.logicalMaximum < 0)));
if (!iStatus)
iStatus = HIDPostProcessRIValue (ptReportItem, &iValue);
if (iStatus != kHIDSuccess)
return iStatus;
iStatus = HIDScaleUsageValueIn(ptReportItem,iValue,&iValue);
*piUsageValue = iValue;
return iStatus;
}
}
}
if (bIncompatibleReport && !butNotReally)
return kHIDIncompatibleReportErr;
return kHIDUsageNotFoundErr;
}