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)
21 }HID_COLLECTION_CONTEXT
, *PHID_COLLECTION_CONTEXT
;
24 HidParser_CalculateCollectionSize(
25 IN PHID_COLLECTION Collection
)
27 ULONG Size
= 0, Index
;
29 Size
= sizeof(HID_COLLECTION
);
32 // add size required for the number of report items
34 for(Index
= 0; Index
< Collection
->ReportCount
; Index
++)
39 ASSERT(Collection
->Reports
[Index
]->ItemCount
);
40 Size
+= sizeof(HID_REPORT
) + Collection
->Reports
[Index
]->ItemCount
* sizeof(HID_REPORT_ITEM
);
44 // calculate size for sub collections
46 for(Index
= 0; Index
< Collection
->NodeCount
; Index
++)
48 Size
+= HidParser_CalculateCollectionSize(Collection
->Nodes
[Index
]);
52 // append size for the offset
54 Size
+= (Collection
->ReportCount
+ Collection
->NodeCount
) * sizeof(ULONG
);
63 HidParser_CalculateContextSize(
64 IN PHID_COLLECTION Collection
)
69 // minimum size is the size of the collection
71 Size
= HidParser_CalculateCollectionSize(Collection
);
74 // append collection context size
76 Size
+= sizeof(HID_COLLECTION_CONTEXT
);
81 HidParser_StoreCollection(
82 IN PHID_PARSER Parser
,
83 IN PHID_COLLECTION Collection
,
84 IN PHID_COLLECTION_CONTEXT CollectionContext
,
85 IN ULONG CurrentOffset
)
91 PHID_COLLECTION TargetCollection
;
94 // backup initial offset
96 InitialOffset
= CurrentOffset
;
99 // get target collection
101 TargetCollection
= (PHID_COLLECTION
)(&CollectionContext
->RawData
[CurrentOffset
]);
104 // first copy the collection details
106 Parser
->Copy(TargetCollection
, Collection
, sizeof(HID_COLLECTION
));
109 // calulcate collection size
111 CollectionSize
= sizeof(HID_COLLECTION
) + sizeof(ULONG
) * (Collection
->ReportCount
+ Collection
->NodeCount
);
116 CurrentOffset
+= CollectionSize
;
121 ASSERT(CurrentOffset
< CollectionContext
->Size
);
124 // first store the report items
126 for(Index
= 0; Index
< Collection
->ReportCount
; Index
++)
129 // calculate report size
131 ReportSize
= sizeof(HID_REPORT
) + Collection
->Reports
[Index
]->ItemCount
* sizeof(HID_REPORT_ITEM
);
136 ASSERT(CurrentOffset
+ ReportSize
< CollectionContext
->Size
);
141 Parser
->Copy(&CollectionContext
->RawData
[CurrentOffset
], Collection
->Reports
[Index
], ReportSize
);
144 // store offset to report item
146 TargetCollection
->Offsets
[Index
] = CurrentOffset
;
149 // move to next offset
151 CurrentOffset
+= ReportSize
;
154 ASSERT(CurrentOffset
<= CollectionContext
->Size
);
157 // now store the sub collections
159 for(Index
= 0; Index
< Collection
->NodeCount
; Index
++)
164 TargetCollection
->Offsets
[Collection
->NodeCount
+ Index
] = CurrentOffset
;
167 // store sub collections
169 CurrentOffset
+= HidParser_StoreCollection(Parser
, Collection
->Nodes
[Index
], CollectionContext
, CurrentOffset
);
174 ASSERT(CurrentOffset
< CollectionContext
->Size
);
178 // return size of collection
180 return CurrentOffset
- InitialOffset
;
184 HidParser_BuildCollectionContext(
185 IN PHID_PARSER Parser
,
186 IN PHID_COLLECTION RootCollection
,
188 IN ULONG ContextSize
)
190 PHID_COLLECTION_CONTEXT CollectionContext
;
191 ULONG CollectionSize
;
196 CollectionContext
= (PHID_COLLECTION_CONTEXT
)Context
;
197 CollectionContext
->Size
= ContextSize
;
202 CollectionSize
= HidParser_StoreCollection(Parser
, RootCollection
, CollectionContext
, 0);
207 ASSERT(CollectionSize
+ sizeof(HID_COLLECTION_CONTEXT
) == ContextSize
);
209 DPRINT1("CollectionContext %p\n", CollectionContext
);
210 DPRINT1("CollectionContext RawData %p\n", CollectionContext
->RawData
);
211 DPRINT1("CollectionContext Size %lu\n", CollectionContext
->Size
);
216 return HIDPARSER_STATUS_SUCCESS
;
220 HidParser_SearchReportInCollection(
221 IN PHID_COLLECTION_CONTEXT CollectionContext
,
222 IN PHID_COLLECTION Collection
,
227 PHID_COLLECTION SubCollection
;
230 // search first in local array
232 for(Index
= 0; Index
< Collection
->ReportCount
; Index
++)
237 Report
= (PHID_REPORT
)(CollectionContext
->RawData
+ Collection
->Offsets
[Index
]);
238 if (Report
->Type
== ReportType
)
248 // now search in sub collections
250 for(Index
= 0; Index
< Collection
->NodeCount
; Index
++)
255 SubCollection
= (PHID_COLLECTION
)(CollectionContext
->RawData
+ Collection
->Offsets
[Collection
->NodeCount
+ Index
]);
258 // recursively search collection
260 Report
= HidParser_SearchReportInCollection(CollectionContext
, SubCollection
, ReportType
);
277 HidParser_GetReportInCollection(
281 PHID_COLLECTION_CONTEXT CollectionContext
= (PHID_COLLECTION_CONTEXT
)Context
;
286 return HidParser_SearchReportInCollection(CollectionContext
, (PHID_COLLECTION
)&CollectionContext
->RawData
, ReportType
);
290 HidParser_GetCollectionFromContext(
293 PHID_COLLECTION_CONTEXT CollectionContext
= (PHID_COLLECTION_CONTEXT
)Context
;
296 // return root collection
298 return (PHID_COLLECTION
)CollectionContext
->RawData
;
302 HidParser_GetCollectionCount(
303 IN PHID_COLLECTION_CONTEXT CollectionContext
,
304 IN PHID_COLLECTION Collection
)
307 ULONG Count
= Collection
->NodeCount
;
308 PHID_COLLECTION SubCollection
;
310 for(Index
= 0; Index
< Collection
->NodeCount
; Index
++)
313 // get offset to sub collection
315 SubCollection
= (PHID_COLLECTION
)(CollectionContext
->RawData
+ Collection
->Offsets
[Collection
->NodeCount
+ Index
]);
318 // count collection for sub nodes
320 Count
+= HidParser_GetCollectionCount(CollectionContext
, SubCollection
);
330 HidParser_GetTotalCollectionCount(
333 PHID_COLLECTION_CONTEXT CollectionContext
;
336 // get parser context
338 CollectionContext
= (PHID_COLLECTION_CONTEXT
)Context
;
343 return HidParser_GetCollectionCount(CollectionContext
, (PHID_COLLECTION
)CollectionContext
->RawData
);