2 * PROJECT: ReactOS HID Parser Library
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: lib/drivers/hidparser/context.c
7 * Michael Martin (michael.martin@reactos.org)
8 * Johannes Anderwald (johannes.anderwald@reactos.org)
23 }HID_COLLECTION_CONTEXT
, *PHID_COLLECTION_CONTEXT
;
26 HidParser_CalculateCollectionSize(
27 IN PHID_COLLECTION Collection
)
29 ULONG Size
= 0, Index
;
31 Size
= sizeof(HID_COLLECTION
);
34 // add size required for the number of report items
36 for(Index
= 0; Index
< Collection
->ReportCount
; Index
++)
41 ASSERT(Collection
->Reports
[Index
]->ItemCount
);
42 Size
+= sizeof(HID_REPORT
) + Collection
->Reports
[Index
]->ItemCount
* sizeof(HID_REPORT_ITEM
);
46 // calculate size for sub collections
48 for(Index
= 0; Index
< Collection
->NodeCount
; Index
++)
50 Size
+= HidParser_CalculateCollectionSize(Collection
->Nodes
[Index
]);
54 // append size for the offset
56 Size
+= (Collection
->ReportCount
+ Collection
->NodeCount
) * sizeof(ULONG
);
65 HidParser_CalculateContextSize(
66 IN PHID_COLLECTION Collection
)
71 // minimum size is the size of the collection
73 Size
= HidParser_CalculateCollectionSize(Collection
);
76 // append collection context size
78 Size
+= sizeof(HID_COLLECTION_CONTEXT
);
83 HidParser_StoreCollection(
84 IN PHID_PARSER Parser
,
85 IN PHID_COLLECTION Collection
,
86 IN PHID_COLLECTION_CONTEXT CollectionContext
,
87 IN ULONG CurrentOffset
)
93 PHID_COLLECTION TargetCollection
;
96 // backup initial offset
98 InitialOffset
= CurrentOffset
;
101 // get target collection
103 TargetCollection
= (PHID_COLLECTION
)(&CollectionContext
->RawData
[CurrentOffset
]);
106 // first copy the collection details
108 Parser
->Copy(TargetCollection
, Collection
, sizeof(HID_COLLECTION
));
111 // calulcate collection size
113 CollectionSize
= sizeof(HID_COLLECTION
) + sizeof(ULONG
) * (Collection
->ReportCount
+ Collection
->NodeCount
);
118 CurrentOffset
+= CollectionSize
;
123 ASSERT(CurrentOffset
< CollectionContext
->Size
);
126 // first store the report items
128 for(Index
= 0; Index
< Collection
->ReportCount
; Index
++)
131 // calculate report size
133 ReportSize
= sizeof(HID_REPORT
) + Collection
->Reports
[Index
]->ItemCount
* sizeof(HID_REPORT_ITEM
);
138 ASSERT(CurrentOffset
+ ReportSize
< CollectionContext
->Size
);
143 Parser
->Copy(&CollectionContext
->RawData
[CurrentOffset
], Collection
->Reports
[Index
], ReportSize
);
146 // store offset to report item
148 TargetCollection
->Offsets
[Index
] = CurrentOffset
;
151 // move to next offset
153 CurrentOffset
+= ReportSize
;
156 ASSERT(CurrentOffset
<= CollectionContext
->Size
);
159 // now store the sub collections
161 for(Index
= 0; Index
< Collection
->NodeCount
; Index
++)
166 TargetCollection
->Offsets
[Collection
->ReportCount
+ Index
] = CurrentOffset
;
169 // store sub collections
171 CurrentOffset
+= HidParser_StoreCollection(Parser
, Collection
->Nodes
[Index
], CollectionContext
, CurrentOffset
);
176 ASSERT(CurrentOffset
< CollectionContext
->Size
);
180 // return size of collection
182 return CurrentOffset
- InitialOffset
;
186 HidParser_BuildCollectionContext(
187 IN PHID_PARSER Parser
,
188 IN PHID_COLLECTION RootCollection
,
190 IN ULONG ContextSize
)
192 PHID_COLLECTION_CONTEXT CollectionContext
;
193 ULONG CollectionSize
;
198 CollectionContext
= (PHID_COLLECTION_CONTEXT
)Context
;
199 CollectionContext
->Size
= ContextSize
;
204 CollectionSize
= HidParser_StoreCollection(Parser
, RootCollection
, CollectionContext
, 0);
209 ASSERT(CollectionSize
+ sizeof(HID_COLLECTION_CONTEXT
) == ContextSize
);
211 DPRINT("CollectionContext %p\n", CollectionContext
);
212 DPRINT("CollectionContext RawData %p\n", CollectionContext
->RawData
);
213 DPRINT("CollectionContext Size %lu\n", CollectionContext
->Size
);
218 return HIDPARSER_STATUS_SUCCESS
;
222 HidParser_SearchReportInCollection(
223 IN PHID_COLLECTION_CONTEXT CollectionContext
,
224 IN PHID_COLLECTION Collection
,
229 PHID_COLLECTION SubCollection
;
232 // search first in local array
234 for(Index
= 0; Index
< Collection
->ReportCount
; Index
++)
239 Report
= (PHID_REPORT
)(CollectionContext
->RawData
+ Collection
->Offsets
[Index
]);
240 if (Report
->Type
== ReportType
)
250 // now search in sub collections
252 for(Index
= 0; Index
< Collection
->NodeCount
; Index
++)
257 SubCollection
= (PHID_COLLECTION
)(CollectionContext
->RawData
+ Collection
->Offsets
[Collection
->ReportCount
+ Index
]);
260 // recursively search collection
262 Report
= HidParser_SearchReportInCollection(CollectionContext
, SubCollection
, ReportType
);
279 HidParser_GetReportInCollection(
283 PHID_COLLECTION_CONTEXT CollectionContext
= (PHID_COLLECTION_CONTEXT
)Context
;
288 return HidParser_SearchReportInCollection(CollectionContext
, (PHID_COLLECTION
)&CollectionContext
->RawData
, ReportType
);
292 HidParser_GetCollectionFromContext(
295 PHID_COLLECTION_CONTEXT CollectionContext
= (PHID_COLLECTION_CONTEXT
)Context
;
298 // return root collection
300 return (PHID_COLLECTION
)CollectionContext
->RawData
;
304 HidParser_GetCollectionCount(
305 IN PHID_COLLECTION_CONTEXT CollectionContext
,
306 IN PHID_COLLECTION Collection
)
309 ULONG Count
= Collection
->NodeCount
;
310 PHID_COLLECTION SubCollection
;
312 for(Index
= 0; Index
< Collection
->NodeCount
; Index
++)
315 // get offset to sub collection
317 SubCollection
= (PHID_COLLECTION
)(CollectionContext
->RawData
+ Collection
->Offsets
[Collection
->ReportCount
+ Index
]);
320 // count collection for sub nodes
322 Count
+= HidParser_GetCollectionCount(CollectionContext
, SubCollection
);
332 HidParser_GetTotalCollectionCount(
335 PHID_COLLECTION_CONTEXT CollectionContext
;
338 // get parser context
340 CollectionContext
= (PHID_COLLECTION_CONTEXT
)Context
;
345 return HidParser_GetCollectionCount(CollectionContext
, (PHID_COLLECTION
)CollectionContext
->RawData
);