Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | #include "drmproperty.h" |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 18 | #include "drmdevice.h" |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 19 | |
| 20 | #include <errno.h> |
| 21 | #include <stdint.h> |
| 22 | #include <string> |
| 23 | |
| 24 | #include <xf86drmMode.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | DrmProperty::DrmPropertyEnum::DrmPropertyEnum(drm_mode_property_enum *e) |
| 29 | : value_(e->value), name_(e->name) { |
| 30 | } |
| 31 | |
| 32 | DrmProperty::DrmPropertyEnum::~DrmPropertyEnum() { |
| 33 | } |
| 34 | |
| 35 | DrmProperty::DrmProperty(drmModePropertyPtr p, uint64_t value) |
Sean Paul | 1eb6006 | 2015-05-19 08:10:50 -0700 | [diff] [blame] | 36 | : id_(0), type_(DRM_PROPERTY_TYPE_INVALID), flags_(0), name_("") { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 37 | Init(p, value); |
| 38 | } |
| 39 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 40 | void DrmProperty::Init(drmModePropertyPtr p, uint64_t value) { |
| 41 | id_ = p->prop_id; |
| 42 | flags_ = p->flags; |
| 43 | name_ = p->name; |
| 44 | value_ = value; |
| 45 | |
| 46 | for (int i = 0; i < p->count_values; ++i) |
| 47 | values_.push_back(p->values[i]); |
| 48 | |
| 49 | for (int i = 0; i < p->count_enums; ++i) |
| 50 | enums_.push_back(DrmPropertyEnum(&p->enums[i])); |
| 51 | |
| 52 | for (int i = 0; i < p->count_blobs; ++i) |
| 53 | blob_ids_.push_back(p->blob_ids[i]); |
| 54 | |
| 55 | if (flags_ & DRM_MODE_PROP_RANGE) |
| 56 | type_ = DRM_PROPERTY_TYPE_INT; |
| 57 | else if (flags_ & DRM_MODE_PROP_ENUM) |
| 58 | type_ = DRM_PROPERTY_TYPE_ENUM; |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 59 | else if (flags_ & DRM_MODE_PROP_OBJECT) |
| 60 | type_ = DRM_PROPERTY_TYPE_OBJECT; |
Sean Paul | 85c58c6 | 2015-07-08 10:43:54 -0400 | [diff] [blame] | 61 | else if (flags_ & DRM_MODE_PROP_BLOB) |
| 62 | type_ = DRM_PROPERTY_TYPE_BLOB; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | uint32_t DrmProperty::id() const { |
| 66 | return id_; |
| 67 | } |
| 68 | |
| 69 | std::string DrmProperty::name() const { |
| 70 | return name_; |
| 71 | } |
| 72 | |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame^] | 73 | std::tuple<int, uint64_t> DrmProperty::value() const { |
| 74 | if (type_ == DRM_PROPERTY_TYPE_BLOB) |
| 75 | return std::make_tuple(0, value_); |
Sean Paul | 85c58c6 | 2015-07-08 10:43:54 -0400 | [diff] [blame] | 76 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 77 | if (values_.size() == 0) |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame^] | 78 | return std::make_tuple(-ENOENT, 0); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 79 | |
| 80 | switch (type_) { |
| 81 | case DRM_PROPERTY_TYPE_INT: |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame^] | 82 | return std::make_tuple(0, value_); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 83 | |
| 84 | case DRM_PROPERTY_TYPE_ENUM: |
| 85 | if (value_ >= enums_.size()) |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame^] | 86 | return std::make_tuple(-ENOENT, 0); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 87 | |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame^] | 88 | return std::make_tuple(0, enums_[value_].value_); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 89 | |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 90 | case DRM_PROPERTY_TYPE_OBJECT: |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame^] | 91 | return std::make_tuple(0, value_); |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 92 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 93 | default: |
Sean Paul | fc0b1da | 2019-03-06 09:48:42 -0500 | [diff] [blame^] | 94 | return std::make_tuple(-EINVAL, 0); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 95 | } |
| 96 | } |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 97 | |
Alexandru Gheorghe | ea1c5e5 | 2018-09-17 10:48:54 +0100 | [diff] [blame] | 98 | bool DrmProperty::immutable() const { |
| 99 | return id_ && (flags_ & DRM_MODE_PROP_IMMUTABLE); |
| 100 | } |
| 101 | |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 102 | std::tuple<uint64_t, int> DrmProperty::GetEnumValueWithName( |
| 103 | std::string name) const { |
| 104 | for (auto it : enums_) { |
| 105 | if (it.name_.compare(name) == 0) { |
| 106 | return std::make_tuple(it.value_, 0); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return std::make_tuple(UINT64_MAX, -EINVAL); |
| 111 | } |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 112 | } // namespace android |