1 #include <string.h>
2 #include <iostream>
3 #include <stdexcept>
5 #include <xf86drm.h>
6 #include <xf86drmMode.h>
8 #include "kms++.h"
10 using namespace std;
12 namespace kms
13 {
15 DrmObject::DrmObject(Card& card, uint32_t object_type)
16 :m_card(card), m_id(-1), m_object_type(object_type), m_idx(0)
17 {
18 }
20 DrmObject::DrmObject(Card& card, uint32_t id, uint32_t object_type, uint32_t idx)
21 :m_card(card), m_id(id), m_object_type(object_type), m_idx(idx)
22 {
23 refresh_props();
24 }
26 DrmObject::~DrmObject()
27 {
29 }
31 void DrmObject::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 DrmObject::get_prop_value(uint32_t id) const
49 {
50 return m_prop_values.at(id);
51 }
53 uint64_t DrmObject::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 int DrmObject::set_prop_value(uint32_t id, uint64_t value)
65 {
66 return drmModeObjectSetProperty(card().fd(), this->id(), this->object_type(), id, value);
67 }
69 int DrmObject::set_prop_value(const string &name, uint64_t value)
70 {
71 Property* prop = card().get_prop(name);
73 if (prop == nullptr)
74 throw invalid_argument("property not found: " + name);
76 return set_prop_value(prop->id(), value);
77 }
79 void DrmObject::set_id(uint32_t id)
80 {
81 m_id = id;
82 }
83 }