2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "DrmProperty.h"
19 #include <xf86drmMode.h>
25 #include "DrmDevice.h"
29 DrmProperty::DrmPropertyEnum::DrmPropertyEnum(drm_mode_property_enum *e)
30 : value_(e->value), name_(e->name) {
33 DrmProperty::DrmProperty(drmModePropertyPtr p, uint64_t value) {
37 void DrmProperty::Init(drmModePropertyPtr p, uint64_t value) {
43 for (int i = 0; i < p->count_values; ++i)
44 values_.emplace_back(p->values[i]);
46 for (int i = 0; i < p->count_enums; ++i)
47 enums_.emplace_back(DrmPropertyEnum(&p->enums[i]));
49 for (int i = 0; i < p->count_blobs; ++i)
50 blob_ids_.emplace_back(p->blob_ids[i]);
52 if (flags_ & DRM_MODE_PROP_RANGE)
53 type_ = DRM_PROPERTY_TYPE_INT;
54 else if (flags_ & DRM_MODE_PROP_ENUM)
55 type_ = DRM_PROPERTY_TYPE_ENUM;
56 else if (flags_ & DRM_MODE_PROP_OBJECT)
57 type_ = DRM_PROPERTY_TYPE_OBJECT;
58 else if (flags_ & DRM_MODE_PROP_BLOB)
59 type_ = DRM_PROPERTY_TYPE_BLOB;
60 else if (flags_ & DRM_MODE_PROP_BITMASK)
61 type_ = DRM_PROPERTY_TYPE_BITMASK;
64 uint32_t DrmProperty::id() const {
68 std::string DrmProperty::name() const {
72 std::tuple<int, uint64_t> DrmProperty::value() const {
73 if (type_ == DRM_PROPERTY_TYPE_BLOB)
74 return std::make_tuple(0, value_);
77 return std::make_tuple(-ENOENT, 0);
80 case DRM_PROPERTY_TYPE_INT:
81 return std::make_tuple(0, value_);
83 case DRM_PROPERTY_TYPE_ENUM:
84 if (value_ >= enums_.size())
85 return std::make_tuple(-ENOENT, 0);
87 return std::make_tuple(0, enums_[value_].value_);
89 case DRM_PROPERTY_TYPE_OBJECT:
90 return std::make_tuple(0, value_);
92 case DRM_PROPERTY_TYPE_BITMASK:
94 return std::make_tuple(-EINVAL, 0);
98 bool DrmProperty::is_immutable() const {
99 return id_ && (flags_ & DRM_MODE_PROP_IMMUTABLE);
102 bool DrmProperty::is_range() const {
103 return id_ && (flags_ & DRM_MODE_PROP_RANGE);
106 std::tuple<int, uint64_t> DrmProperty::range_min() const {
108 return std::make_tuple(-EINVAL, 0);
110 return std::make_tuple(-ENOENT, 0);
112 return std::make_tuple(0, values_[0]);
115 std::tuple<int, uint64_t> DrmProperty::range_max() const {
117 return std::make_tuple(-EINVAL, 0);
118 if (values_.size() < 2)
119 return std::make_tuple(-ENOENT, 0);
121 return std::make_tuple(0, values_[1]);
124 std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName(
125 const std::string &name) const {
126 for (const auto &it : enums_) {
127 if (it.name_ == name) {
128 return std::make_tuple(it.value_, 0);
132 return std::make_tuple(UINT64_MAX, -EINVAL);
134 } // namespace android