Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / device / hid / hid_report_descriptor.cc
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 HidReportDescriptor::HidReportDescriptor(const uint8_t* bytes, size_t size) {
12   size_t header_index = 0;
13   HidReportDescriptorItem* item = NULL;
14   while (header_index < size) {
15     item = new HidReportDescriptorItem(&bytes[header_index], item);
16     items_.push_back(linked_ptr<HidReportDescriptorItem>(item));
17     header_index += item->GetSize();
18   }
19 }
20
21 HidReportDescriptor::~HidReportDescriptor() {}
22
23 void HidReportDescriptor::GetTopLevelCollections(
24     std::vector<HidUsageAndPage>* topLevelCollections) {
25   DCHECK(topLevelCollections);
26   STLClearObject(topLevelCollections);
27
28   for (std::vector<linked_ptr<HidReportDescriptorItem> >::const_iterator
29            items_iter = items().begin();
30        items_iter != items().end();
31        ++items_iter) {
32     linked_ptr<HidReportDescriptorItem> item = *items_iter;
33
34     bool isTopLevelCollection =
35         item->tag() == HidReportDescriptorItem::kTagCollection &&
36         item->parent() == NULL;
37
38     if (isTopLevelCollection) {
39       uint16_t collection_usage = 0;
40       HidUsageAndPage::Page collection_usage_page =
41           HidUsageAndPage::kPageUndefined;
42
43       HidReportDescriptorItem* usage = item->previous();
44       if (usage && usage->tag() == HidReportDescriptorItem::kTagUsage) {
45         collection_usage = usage->GetShortData();
46       }
47
48       HidReportDescriptorItem* usage_page = usage->previous();
49       if (usage_page &&
50           usage_page->tag() == HidReportDescriptorItem::kTagUsagePage) {
51         collection_usage_page =
52             (HidUsageAndPage::Page)usage_page->GetShortData();
53       }
54
55       topLevelCollections->push_back(
56           HidUsageAndPage(collection_usage, collection_usage_page));
57     }
58   }
59 }
60
61 }  // namespace device