blob: 378e3a28463be52392267ddc8a89f0e7ea9ac3e6 [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)
Sean Paul1eb60062015-05-19 08:10:50 -070036 : id_(0), type_(DRM_PROPERTY_TYPE_INVALID), flags_(0), name_("") {
Sean Paul6a55e9f2015-04-30 15:31:06 -040037 Init(p, value);
38}
39
Sean Paul1eb60062015-05-19 08:10:50 -070040DrmProperty::DrmProperty()
41 : id_(0),
42 type_(DRM_PROPERTY_TYPE_INVALID),
43 flags_(0),
44 name_(""),
45 value_(0) {
Sean Paul6a55e9f2015-04-30 15:31:06 -040046}
47
48DrmProperty::~DrmProperty() {
49}
50
51void DrmProperty::Init(drmModePropertyPtr p, uint64_t value) {
52 id_ = p->prop_id;
53 flags_ = p->flags;
54 name_ = p->name;
55 value_ = value;
56
57 for (int i = 0; i < p->count_values; ++i)
58 values_.push_back(p->values[i]);
59
60 for (int i = 0; i < p->count_enums; ++i)
61 enums_.push_back(DrmPropertyEnum(&p->enums[i]));
62
63 for (int i = 0; i < p->count_blobs; ++i)
64 blob_ids_.push_back(p->blob_ids[i]);
65
66 if (flags_ & DRM_MODE_PROP_RANGE)
67 type_ = DRM_PROPERTY_TYPE_INT;
68 else if (flags_ & DRM_MODE_PROP_ENUM)
69 type_ = DRM_PROPERTY_TYPE_ENUM;
Sean Paul877be972015-06-03 14:08:27 -040070 else if (flags_ & DRM_MODE_PROP_OBJECT)
71 type_ = DRM_PROPERTY_TYPE_OBJECT;
Sean Paul85c58c62015-07-08 10:43:54 -040072 else if (flags_ & DRM_MODE_PROP_BLOB)
73 type_ = DRM_PROPERTY_TYPE_BLOB;
74
Sean Paul6a55e9f2015-04-30 15:31:06 -040075}
76
77uint32_t DrmProperty::id() const {
78 return id_;
79}
80
81std::string DrmProperty::name() const {
82 return name_;
83}
84
85int DrmProperty::value(uint64_t *value) const {
Sean Paul85c58c62015-07-08 10:43:54 -040086 if (type_ == DRM_PROPERTY_TYPE_BLOB) {
87 *value = value_;
88 return 0;
89 }
90
Sean Paul6a55e9f2015-04-30 15:31:06 -040091 if (values_.size() == 0)
92 return -ENOENT;
93
94 switch (type_) {
95 case DRM_PROPERTY_TYPE_INT:
96 *value = value_;
97 return 0;
98
99 case DRM_PROPERTY_TYPE_ENUM:
100 if (value_ >= enums_.size())
101 return -ENOENT;
102
103 *value = enums_[value_].value_;
104 return 0;
105
Sean Paul877be972015-06-03 14:08:27 -0400106 case DRM_PROPERTY_TYPE_OBJECT:
107 *value = value_;
108 return 0;
109
Sean Paul6a55e9f2015-04-30 15:31:06 -0400110 default:
111 return -EINVAL;
112 }
113}
114}