]> git.saurik.com Git - apple/xnu.git/blob - iokit/Families/IOHIDSystem/IOHIDDescriptorParser/HIDHasUsage.c
xnu-123.5.tar.gz
[apple/xnu.git] / iokit / Families / IOHIDSystem / IOHIDDescriptorParser / HIDHasUsage.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: HIDHasUsage.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 * HidP_UsageFromIndex
56 *
57 * Input:
58 * ptPreparsedData - The Preparsed Data
59 * ptReportItem - The Report Item
60 * usagePage - The usage Page to find
61 * usage - The usage to find
62 * piIndex(optional) - The usage Index pointer (Can be used to tell
63 * which bits in an array correspond to that usage.)
64 * piCount(optional) - The usage Count pointer (Can be used to tell
65 * how many items will be in a report.)
66 * Output:
67 * piIndex - The usage Index
68 * Returns:
69 * The usage
70 *
71 *------------------------------------------------------------------------------
72 */
73 Boolean HIDHasUsage (HIDPreparsedDataRef preparsedDataRef,
74 HIDReportItem *ptReportItem,
75 HIDUsage usagePage, HIDUsage usage,
76 UInt32 *piIndex, UInt32 *piCount)
77 {
78 HIDPreparsedDataPtr ptPreparsedData = (HIDPreparsedDataPtr) preparsedDataRef;
79 int iUsageItem;
80 UInt32 iUsageIndex;
81 int iUsages;
82 int i;
83 UInt32 iCountsLeft;
84 HIDP_UsageItem *ptUsageItem;
85 Boolean bOnPage;
86 /*
87 * Disallow Null Pointers
88 */
89 if ((ptPreparsedData == NULL)
90 || (ptReportItem == NULL))
91 return 0;
92 if (ptPreparsedData->hidTypeIfValid != kHIDOSType)
93 return 0;
94 /*
95 * Look through the usage Items for this usage
96 */
97 iUsageItem = ptReportItem->firstUsageItem;
98 iUsageIndex = 0;
99 for (i=0; i<ptReportItem->usageItemCount; i++)
100 {
101 /*
102 * Each usage Item is either a usage or a usage range
103 */
104 ptUsageItem = &ptPreparsedData->usageItems[iUsageItem++];
105 bOnPage = ((usagePage == 0) || (usagePage == ptUsageItem->usagePage));
106 if (ptUsageItem->isRange)
107 {
108 /*
109 * For usage Ranges
110 * If the index is in the range
111 * then return the usage
112 * Otherwise adjust the index by the size of the range
113 */
114 if ((usage >= ptUsageItem->usageMinimum)
115 && (usage <= ptUsageItem->usageMaximum))
116 {
117 if (piIndex != NULL)
118 *piIndex = iUsageIndex + (ptUsageItem->usageMinimum - usage);
119 /*
120 * If this usage is the last one for this ReportItem
121 * then it gets all of the remaining reportCount
122 */
123 if (piCount != NULL)
124 {
125 // piCount is going to be used to find which element in a button array is
126 // the one that returns the value for that usage.
127 if (((i+1) == ptReportItem->usageItemCount)
128 && (usage == ptUsageItem->usageMaximum))
129 {
130 // Hmm, the same logic in the non-range case below was wrong. But things
131 // seem to be working for finding buttons, so i am not changing it here.
132 // However, we have made some changes to range calculations that may no
133 // longer require that -1 here either. Heads up!
134 iCountsLeft = ptReportItem->globals.reportCount - iUsageIndex - 1;
135 if (iCountsLeft > 1)
136 *piCount = iCountsLeft;
137 else
138 *piCount = 1;
139 }
140 else
141 *piCount = 1;
142 }
143 if (bOnPage)
144 return true;
145 }
146 iUsages = ptUsageItem->usageMaximum - ptUsageItem->usageMinimum;
147 if (iUsages < 0)
148 iUsages = -iUsages;
149 iUsages++; // Add off by one adjustment AFTER sign correction.
150 iUsageIndex += iUsages;
151 }
152 else
153 {
154 /*
155 * For Usages
156 * If the index is zero
157 * then return this usage
158 * Otherwise one less to index through
159 */
160 if (usage == ptUsageItem->usage)
161 {
162 if (piIndex != NULL)
163 *piIndex = iUsageIndex;
164 if (piCount != NULL)
165 {
166 if ((i+1) == ptReportItem->usageItemCount)
167 {
168 // Keithen does not understand the logic of iCountsLeft.
169 // In Radar #2579612 we come through here for HIDGetUsageValueArray
170 // and HIDGetSpecificValueCaps. In both cases piCount that is returned
171 // should be the reportCount without the -1.
172 // iCountsLeft = ptReportItem->globals.reportCount - iUsageIndex - 1;
173 iCountsLeft = ptReportItem->globals.reportCount - iUsageIndex;
174 if (iCountsLeft > 1)
175 *piCount = iCountsLeft;
176 else
177 *piCount = 1;
178 }
179 else
180 *piCount = 1;
181 }
182 if (bOnPage)
183 return true;
184 }
185 iUsageIndex++;
186 }
187 }
188 return false;
189 }