#include "HIDLib.h"
OSStatus HIDSetButton (HIDReportType reportType,
HIDUsage usagePage,
UInt32 collection,
HIDUsage usage,
HIDPreparsedDataRef preparsedDataRef,
void * report,
ByteCount reportLength)
{
HIDPreparsedDataPtr ptPreparsedData = (HIDPreparsedDataPtr) preparsedDataRef;
HIDCollection *ptCollection;
HIDReportItem *ptReportItem;
OSStatus iStatus;
int iR, iX;
SInt32 data;
int iStart;
int iReportItem;
UInt32 iUsageIndex;
Boolean bIncompatibleReport = false;
Boolean butNotReally = false;
if ((ptPreparsedData == NULL)
|| (report == NULL))
return kHIDNullPointerErr;
if (ptPreparsedData->hidTypeIfValid != kHIDOSType)
return kHIDInvalidPreparsedDataErr;
if ((collection < 0) || (collection >= ptPreparsedData->collectionCount))
return kHIDBadParameterErr;
ptCollection = &ptPreparsedData->collections[collection];
for (iR=0; iR<ptCollection->reportItemCount; iR++)
{
iReportItem = ptCollection->firstReportItem + iR;
ptReportItem = &ptPreparsedData->reportItems[iReportItem];
if (HIDIsButton(ptReportItem, preparsedDataRef)
&& HIDHasUsage(preparsedDataRef,ptReportItem,usagePage,usage,&iUsageIndex,NULL))
{
iStatus = HIDCheckReport(reportType,preparsedDataRef,ptReportItem,report,reportLength);
if (iStatus == kHIDIncompatibleReportErr)
bIncompatibleReport = true;
else if (iStatus != kHIDSuccess)
return iStatus;
else
{
butNotReally = true;
if ((ptReportItem->dataModes & kHIDDataArrayBit) == kHIDDataArray)
{
for (iX=0; iX<ptReportItem->globals.reportCount; iX++)
{
iStart = ptReportItem->startBit + (ptReportItem->globals.reportSize * iX);
iStatus = HIDGetData(report, reportLength, iStart,
ptReportItem->globals.reportSize, &data, true);
if (!iStatus)
iStatus = HIDPostProcessRIValue (ptReportItem, &data);
if (iStatus != kHIDSuccess)
return iStatus;
if (data == 0)
return HIDPutData(report, reportLength, iStart,
ptReportItem->globals.reportSize,
iUsageIndex + ptReportItem->globals.logicalMinimum);
}
return kHIDBufferTooSmallErr;
}
else if (ptReportItem->globals.reportSize == 1)
{
iStart = ptReportItem->startBit + (ptReportItem->globals.reportSize * iUsageIndex);
iStatus = HIDPutData(report, reportLength, iStart, ptReportItem->globals.reportSize, -1);
if (iStatus != kHIDSuccess)
return iStatus;
return kHIDSuccess;
}
}
}
}
if (bIncompatibleReport && !butNotReally)
return kHIDIncompatibleReportErr;
return kHIDUsageNotFoundErr;
}
OSStatus
HIDSetButtons (HIDReportType reportType,
HIDUsage usagePage,
UInt32 collection,
HIDUsage * usageList,
UInt32 * usageListSize,
HIDPreparsedDataRef preparsedDataRef,
void * report,
ByteCount reportLength)
{
HIDPreparsedDataPtr ptPreparsedData = (HIDPreparsedDataPtr) preparsedDataRef;
OSStatus iStatus;
int iUsages;
int usage;
if ((ptPreparsedData == NULL)
|| (usageList == NULL)
|| (usageListSize == NULL)
|| (report == NULL))
return kHIDNullPointerErr;
if (ptPreparsedData->hidTypeIfValid != kHIDOSType)
return kHIDInvalidPreparsedDataErr;
iUsages = *usageListSize;
for (usage=0; usage<iUsages; usage++)
{
*usageListSize = usage;
iStatus = HIDSetButton(reportType, usagePage, collection,
usageList[usage], preparsedDataRef,
report, reportLength);
if (iStatus != kHIDSuccess)
return iStatus;
}
return kHIDSuccess;
}