1 #include <cstring>
2 #include <iostream>
3 #include <stdexcept>
4
5 #include <xf86drm.h>
6 #include <xf86drmMode.h>
7
8 #include <kms++/kms++.h>
9
10 using namespace std;
11
12 namespace kms
13 {
DrmPropObject(Card & card,uint32_t object_type)14 DrmPropObject::DrmPropObject(Card& card, uint32_t object_type)
15 : DrmObject(card, object_type)
16 {
17 }
18
DrmPropObject(Card & card,uint32_t id,uint32_t object_type,uint32_t idx)19 DrmPropObject::DrmPropObject(Card& card, uint32_t id, uint32_t object_type, uint32_t idx)
20 : DrmObject(card, id, object_type, idx)
21 {
22 refresh_props();
23 }
24
~DrmPropObject()25 DrmPropObject::~DrmPropObject()
26 {
27 }
28
refresh_props()29 void DrmPropObject::refresh_props()
30 {
31 auto props = drmModeObjectGetProperties(card().fd(), this->id(), this->object_type());
32
33 if (props == nullptr)
34 return;
35
36 for (unsigned i = 0; i < props->count_props; ++i) {
37 uint32_t prop_id = props->props[i];
38 uint64_t prop_value = props->prop_values[i];
39
40 m_prop_values[prop_id] = prop_value;
41 }
42
43 drmModeFreeObjectProperties(props);
44 }
45
get_prop(const string & name) const46 Property* DrmPropObject::get_prop(const string& name) const
47 {
48 for (auto pair : m_prop_values) {
49 auto prop = card().get_prop(pair.first);
50
51 if (name == prop->name())
52 return prop;
53 }
54
55 return nullptr;
56 }
57
get_prop_value(uint32_t id) const58 uint64_t DrmPropObject::get_prop_value(uint32_t id) const
59 {
60 return m_prop_values.at(id);
61 }
62
get_prop_value(const string & name) const63 uint64_t DrmPropObject::get_prop_value(const string& name) const
64 {
65 for (auto pair : m_prop_values) {
66 auto prop = card().get_prop(pair.first);
67 if (name == prop->name())
68 return m_prop_values.at(prop->id());
69 }
70
71 throw invalid_argument("property not found: " + name);
72 }
73
get_prop_value_as_blob(const string & name) const74 unique_ptr<Blob> DrmPropObject::get_prop_value_as_blob(const string& name) const
75 {
76 uint32_t blob_id = (uint32_t)get_prop_value(name);
77
78 return unique_ptr<Blob>(new Blob(card(), blob_id));
79 }
80
set_prop_value(Property * prop,uint64_t value)81 int DrmPropObject::set_prop_value(Property* prop, uint64_t value)
82 {
83 return drmModeObjectSetProperty(card().fd(), this->id(), this->object_type(), prop->id(), value);
84 }
85
set_prop_value(uint32_t id,uint64_t value)86 int DrmPropObject::set_prop_value(uint32_t id, uint64_t value)
87 {
88 return drmModeObjectSetProperty(card().fd(), this->id(), this->object_type(), id, value);
89 }
90
set_prop_value(const string & name,uint64_t value)91 int DrmPropObject::set_prop_value(const string& name, uint64_t value)
92 {
93 Property* prop = get_prop(name);
94
95 if (prop == nullptr)
96 throw invalid_argument("property not found: " + name);
97
98 return set_prop_value(prop->id(), value);
99 }
100
101 } // namespace kms
102