1/*
2 * @APPLE_LICENSE_HEADER_START@
3 *
4 * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24	File:		HIDUsageAndPageFromIndex.c
25
26	Contains:	xxx put contents here xxx
27
28	Version:	xxx put version here xxx
29
30	Copyright:	� 1999-2000 by Apple Computer, Inc., all rights reserved.
31
32	File Ownership:
33
34		DRI:				xxx put dri here xxx
35
36		Other Contact:		xxx put other contact here xxx
37
38		Technology:			xxx put technology here xxx
39
40	Writers:
41
42		(KH)	Keithen Hayenga
43		(BWS)	Brent Schorsch
44
45	Change History (most recent first):
46
47	  <USB2>	12/12/00	KH		range count off by 1.
48	  <USB1>	  3/5/99	BWS		first checked in
49*/
50
51#include "HIDLib.h"
52
53/*
54 *------------------------------------------------------------------------------
55 *
56 * HIDUsageAndPageFromIndex
57 *
58 *	 Input:
59 *			  ptPreparsedData		- The Preparsed Data
60 *			  ptReportItem			- The Report Item
61 *			  index				   - The usage Index
62 *			  ptUsageAndPage		- The usage And Page
63 *	 Output:
64 *	 Returns:
65 *
66 *------------------------------------------------------------------------------
67*/
68void HIDUsageAndPageFromIndex (HIDPreparsedDataRef preparsedDataRef,
69								 HIDReportItem *ptReportItem, UInt32 index,
70								 HIDUsageAndPage *ptUsageAndPage)
71{
72	HIDPreparsedDataPtr ptPreparsedData = (HIDPreparsedDataPtr) preparsedDataRef;
73	HIDP_UsageItem *ptUsageItem = NULL;
74	int iUsageItem;
75	int iUsages;
76	int i;
77
78/*
79 *	Disallow NULL Pointers
80*/
81	if (ptUsageAndPage == NULL)
82	{
83		return;	// kHIDNullPointerErr;
84	}
85	if ((ptReportItem == NULL) || (ptPreparsedData == NULL))
86	{
87        ptUsageAndPage->usagePage = 0;
88		return;	// kHIDNullPointerErr;
89	}
90
91/*
92 *	Index through the usage Items for this ReportItem
93*/
94	iUsageItem = ptReportItem->firstUsageItem;
95	for (i=0; i<ptReportItem->usageItemCount; i++)
96	{
97/*
98 *		Each usage Item is either a usage or a usage range
99*/
100		ptUsageItem = &ptPreparsedData->usageItems[iUsageItem++];
101		if (ptUsageItem->isRange)
102		{
103/*
104 *			For usage Ranges
105 *			  If the index is in the range
106 *				then return the usage
107 *			  Otherwise adjust the index by the size of the range
108*/
109			iUsages = ptUsageItem->usageMaximum - ptUsageItem->usageMinimum;
110			if (iUsages < 0)
111				iUsages = -iUsages;
112			iUsages++;		// Add off by one adjustment AFTER sign correction.
113			if (iUsages > index)
114			{
115				ptUsageAndPage->usagePage = ptUsageItem->usagePage;
116				ptUsageAndPage->usage = ptUsageItem->usageMinimum + index;
117				return;
118			}
119			index -= iUsages;
120		}
121		else
122		{
123/*
124 *			For Usages
125 *			If the index is zero
126 *			  then return this usage
127 *			Otherwise one less to index through
128*/
129			if (index-- == 0)
130			{
131				ptUsageAndPage->usagePage = ptUsageItem->usagePage;
132				ptUsageAndPage->usage = ptUsageItem->usage;
133				return;
134			}
135		}
136	}
137	if (ptUsageItem != NULL)
138	{
139		ptUsageAndPage->usagePage = ptUsageItem->usagePage;
140		if (ptUsageItem->isRange)
141			ptUsageAndPage->usage = ptUsageItem->usageMaximum;
142		else
143			ptUsageAndPage->usage = ptUsageItem->usage;
144	}
145}
146