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