OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "device/hid/hid_report_descriptor.h" |
| 6 |
| 7 #include "base/stl_util.h" |
| 8 |
| 9 namespace device { |
| 10 |
| 11 namespace { |
| 12 |
| 13 const char kIndentStep[] = " "; |
| 14 |
| 15 } // namespace |
| 16 |
| 17 HidReportDescriptor::HidReportDescriptor(const uint8_t* bytes, size_t size) { |
| 18 size_t header_index = 0; |
| 19 HidReportDescriptorItem* item = NULL; |
| 20 while (header_index < size) { |
| 21 item = new HidReportDescriptorItem(&bytes[header_index], item); |
| 22 items_.push_back(linked_ptr<HidReportDescriptorItem>(item)); |
| 23 header_index += item->GetSize(); |
| 24 } |
| 25 } |
| 26 |
| 27 HidReportDescriptor::~HidReportDescriptor() {} |
| 28 |
| 29 void HidReportDescriptor::GetTopLevelCollections( |
| 30 std::vector<HidUsageAndPage>* topLevelCollections) { |
| 31 DCHECK(topLevelCollections); |
| 32 STLClearObject(topLevelCollections); |
| 33 |
| 34 for (std::vector<linked_ptr<HidReportDescriptorItem> >::const_iterator |
| 35 items_iter = items().begin(); |
| 36 items_iter != items().end(); |
| 37 ++items_iter) { |
| 38 linked_ptr<HidReportDescriptorItem> item = *items_iter; |
| 39 |
| 40 bool isTopLevelCollection = |
| 41 item->tag() == HidReportDescriptorItem::kTagCollection && |
| 42 item->parent() == NULL; |
| 43 |
| 44 if (isTopLevelCollection) { |
| 45 uint16_t collection_usage = 0; |
| 46 HidUsageAndPage::Page collection_usage_page = |
| 47 HidUsageAndPage::kPageUndefined; |
| 48 |
| 49 HidReportDescriptorItem* usage = item->previous(); |
| 50 if (usage && usage->tag() == HidReportDescriptorItem::kTagUsage) { |
| 51 collection_usage = usage->GetShortData(); |
| 52 } |
| 53 |
| 54 HidReportDescriptorItem* usage_page = usage->previous(); |
| 55 if (usage_page && |
| 56 usage_page->tag() == HidReportDescriptorItem::kTagUsagePage) { |
| 57 collection_usage_page = |
| 58 (HidUsageAndPage::Page)usage_page->GetShortData(); |
| 59 } |
| 60 |
| 61 topLevelCollections->push_back( |
| 62 HidUsageAndPage(collection_usage, collection_usage_page)); |
| 63 } |
| 64 } |
| 65 } |
| 66 |
| 67 std::ostream& operator<<(std::ostream& os, |
| 68 const HidReportDescriptor& descriptor) { |
| 69 for (std::vector<linked_ptr<HidReportDescriptorItem> >::const_iterator |
| 70 items_iter = descriptor.items().begin(); |
| 71 items_iter != descriptor.items().end(); |
| 72 ++items_iter) { |
| 73 linked_ptr<HidReportDescriptorItem> item = *items_iter; |
| 74 size_t indentLevel = item->GetDepth(); |
| 75 for (size_t i = 0; i < indentLevel; i++) |
| 76 os << kIndentStep; |
| 77 os << *item.get() << std::endl; |
| 78 } |
| 79 return os; |
| 80 } |
| 81 |
| 82 } // namespace device |
OLD | NEW |