kms++: organize into subdirs
[android/external-libkmsxx.git] / kms++ / src / drmpropobject.cpp
1 #include <string.h>
2 #include <iostream>
3 #include <stdexcept>
5 #include <xf86drm.h>
6 #include <xf86drmMode.h>
8 #include <kms++/kms++.h>
10 using namespace std;
12 namespace kms
13 {
15 DrmPropObject::DrmPropObject(Card& card, uint32_t object_type)
16         : DrmObject(card, object_type)
17 {
18 }
20 DrmPropObject::DrmPropObject(Card& card, uint32_t id, uint32_t object_type, uint32_t idx)
21         : DrmObject(card, id, object_type, idx)
22 {
23         refresh_props();
24 }
26 DrmPropObject::~DrmPropObject()
27 {
29 }
31 void DrmPropObject::refresh_props()
32 {
33         auto props = drmModeObjectGetProperties(card().fd(), this->id(), this->object_type());
35         if (props == nullptr)
36                 return;
38         for (unsigned i = 0; i < props->count_props; ++i) {
39                 uint32_t prop_id = props->props[i];
40                 uint64_t prop_value = props->prop_values[i];
42                 m_prop_values[prop_id] = prop_value;
43         }
45         drmModeFreeObjectProperties(props);
46 }
48 uint64_t DrmPropObject::get_prop_value(uint32_t id) const
49 {
50         return m_prop_values.at(id);
51 }
53 uint64_t DrmPropObject::get_prop_value(const string& name) const
54 {
55         for (auto pair : m_prop_values) {
56                 auto prop = card().get_prop(pair.first);
57                 if (name == prop->name())
58                         return m_prop_values.at(prop->id());
59         }
61         throw invalid_argument("property not found: " + name);
62 }
64 unique_ptr<Blob> DrmPropObject::get_prop_value_as_blob(const string& name) const
65 {
66         uint32_t blob_id = (uint32_t)get_prop_value(name);
68         return unique_ptr<Blob>(new Blob(card(), blob_id));
69 }
71 int DrmPropObject::set_prop_value(uint32_t id, uint64_t value)
72 {
73         return drmModeObjectSetProperty(card().fd(), this->id(), this->object_type(), id, value);
74 }
76 int DrmPropObject::set_prop_value(const string &name, uint64_t value)
77 {
78         Property* prop = card().get_prop(name);
80         if (prop == nullptr)
81                 throw invalid_argument("property not found: " + name);
83         return set_prop_value(prop->id(), value);
84 }
86 }