blob: 0acea17e81daca9cd6f64940a92acdcba468c526 [file] [log] [blame]
Sean Paul6a55e9f2015-04-30 15:31:06 -04001/*
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"
18#include "drmresources.h"
19
20#include <errno.h>
21#include <stdint.h>
22#include <string>
23
24#include <xf86drmMode.h>
25
26namespace android {
27
28DrmProperty::DrmPropertyEnum::DrmPropertyEnum(drm_mode_property_enum *e)
29 : value_(e->value), name_(e->name) {
30}
31
32DrmProperty::DrmPropertyEnum::~DrmPropertyEnum() {
33}
34
35DrmProperty::DrmProperty(drmModePropertyPtr p, uint64_t value)
36 : id_(0), flags_(0), name_("") {
37 Init(p, value);
38}
39
40DrmProperty::DrmProperty() : id_(0), flags_(0), name_(""), value_(0) {
41}
42
43DrmProperty::~DrmProperty() {
44}
45
46void DrmProperty::Init(drmModePropertyPtr p, uint64_t value) {
47 id_ = p->prop_id;
48 flags_ = p->flags;
49 name_ = p->name;
50 value_ = value;
51
52 for (int i = 0; i < p->count_values; ++i)
53 values_.push_back(p->values[i]);
54
55 for (int i = 0; i < p->count_enums; ++i)
56 enums_.push_back(DrmPropertyEnum(&p->enums[i]));
57
58 for (int i = 0; i < p->count_blobs; ++i)
59 blob_ids_.push_back(p->blob_ids[i]);
60
61 if (flags_ & DRM_MODE_PROP_RANGE)
62 type_ = DRM_PROPERTY_TYPE_INT;
63 else if (flags_ & DRM_MODE_PROP_ENUM)
64 type_ = DRM_PROPERTY_TYPE_ENUM;
65}
66
67uint32_t DrmProperty::id() const {
68 return id_;
69}
70
71std::string DrmProperty::name() const {
72 return name_;
73}
74
75int DrmProperty::value(uint64_t *value) const {
76 if (values_.size() == 0)
77 return -ENOENT;
78
79 switch (type_) {
80 case DRM_PROPERTY_TYPE_INT:
81 *value = value_;
82 return 0;
83
84 case DRM_PROPERTY_TYPE_ENUM:
85 if (value_ >= enums_.size())
86 return -ENOENT;
87
88 *value = enums_[value_].value_;
89 return 0;
90
91 default:
92 return -EINVAL;
93 }
94}
95}