Changeset View
Changeset View
Standalone View
Standalone View
daemon/backends/upower/ddcutilbrightness.cpp
Show All 16 Lines | |||||
17 | * | 17 | * | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include <powerdevil_debug.h> | 20 | #include <powerdevil_debug.h> | ||
21 | #include "ddcutilbrightness.h" | 21 | #include "ddcutilbrightness.h" | ||
22 | 22 | | |||
23 | 23 | | |||
24 | DDCutilBrightness::DDCutilBrightness() | 24 | DDCutilBrightness::DDCutilBrightness() | ||
25 | : m_usedVcp({0x10}) | ||||
25 | { | 26 | { | ||
broulik: Use initializer:
```
DDCutilBrightness::DDCUtilBrightness()
: m_usedVcp({0x10})
``` | |||||
26 | m_setBrightnessEventFilter.setInterval(100); | 27 | m_setBrightnessEventFilter.setInterval(100); | ||
27 | m_setBrightnessEventFilter.setSingleShot(true); | 28 | m_setBrightnessEventFilter.setSingleShot(true); | ||
28 | connect(&m_setBrightnessEventFilter, &QTimer::timeout, this, &DDCutilBrightness::setBrightnessAfterFilter); | 29 | connect(&m_setBrightnessEventFilter, &QTimer::timeout, this, &DDCutilBrightness::setBrightnessAfterFilter); | ||
29 | } | 30 | } | ||
30 | 31 | | |||
31 | void DDCutilBrightness::detect() | 32 | void DDCutilBrightness::detect() | ||
32 | { | 33 | { | ||
33 | #ifndef WITH_DDCUTIL | 34 | #ifdef WITH_DDCUTIL | ||
34 | qCInfo(POWERDEVIL) << "[DDCutilBrightness] compiled without DDC/CI support"; | | |||
35 | return; | | |||
36 | #else | | |||
37 | DDCA_Status rc; | 35 | DDCA_Status rc; | ||
38 | 36 | | |||
39 | qCDebug(POWERDEVIL) << "Check for monitors using ddca_get_displays()..."; | 37 | qCDebug(POWERDEVIL) << "Check for monitors using ddca_get_displays()..."; | ||
40 | // Inquire about detected monitors. | 38 | // Inquire about detected monitors. | ||
broulik: `nullptr` | |||||
41 | DDCA_Display_Info_List * dlist = ddca_get_display_info_list(); | 39 | DDCA_Display_Info_List * dlist = ddca_get_display_info_list(); | ||
42 | qCDebug(POWERDEVIL) << "ddca_get_display_info_list() returned "<< dlist; | 40 | qCDebug(POWERDEVIL) << "ddca_get_display_info_list() returned "<< dlist; | ||
43 | qCInfo(POWERDEVIL) << "[DDCutilBrightness] " << dlist->ct << "display(s) were detected"; | 41 | qCInfo(POWERDEVIL) << "[DDCutilBrightness] " << dlist->ct << "display(s) were detected."; | ||
44 | 42 | | |||
45 | for (int iDisp=0;iDisp<dlist->ct;iDisp++) { | 43 | for (int iDisp = 0; iDisp < dlist->ct; ++iDisp) { | ||
46 | DDCA_Display_Identifier did; | | |||
47 | DDCA_Display_Ref dref; | | |||
48 | DDCA_Display_Handle dh = nullptr; // initialize to avoid clang analyzer warning | 44 | DDCA_Display_Handle dh = nullptr; // initialize to avoid clang analyzer warning | ||
49 | 45 | | |||
50 | qCDebug(POWERDEVIL) << "Create a Display Identifier for display"<<iDisp << | 46 | qCDebug(POWERDEVIL) << "Create a Display Identifier for display"<<iDisp << | ||
51 | " : "<< dlist->info[iDisp].model_name; | 47 | " : "<< dlist->info[iDisp].model_name; | ||
52 | 48 | | |||
53 | m_displayInfoList.append(dlist->info[iDisp]); | 49 | m_displayInfoList.append(dlist->info[iDisp]); | ||
54 | 50 | | |||
55 | rc = ddca_create_dispno_display_identifier(iDisp+1, &did); // ddcutil uses 1 paded indexing for displays | | |||
56 | | ||||
57 | char * did_repr = ddca_did_repr(did); | | |||
58 | | ||||
59 | qCDebug(POWERDEVIL) << "did="<<did_repr; | | |||
60 | | ||||
61 | qCDebug(POWERDEVIL) << "Create a display reference from the display identifier..."; | | |||
62 | rc = ddca_get_display_ref(did, &dref); | | |||
63 | | ||||
64 | if (rc != 0) { | | |||
65 | qCWarning(POWERDEVIL) << "[DDCutilBrightness]: ddct_get_display_ref() returned "<< | | |||
66 | rc<< " ("<<ddca_rc_name(rc) << | | |||
67 | "): "<< ddca_rc_name(rc); | | |||
68 | continue; | | |||
69 | } | | |||
70 | | ||||
71 | char * dref_repr = ddca_dref_repr(dref); | | |||
72 | qCDebug(POWERDEVIL) << "dref= " << dref_repr; | | |||
73 | | ||||
74 | qCDebug(POWERDEVIL) << "Open the display reference, creating a display handle..."; | 51 | qCDebug(POWERDEVIL) << "Open the display reference, creating a display handle..."; | ||
75 | rc = ddca_open_display(dref, &dh); | 52 | rc = ddca_open_display(dlist->info[iDisp].dref, &dh); | ||
76 | if (rc != 0) { | 53 | if (rc != 0) { | ||
77 | qCWarning(POWERDEVIL) << "[DDCutilBrightness]: ddct_open_display"<< rc; | 54 | qCWarning(POWERDEVIL) << "[DDCutilBrightness]: ddct_open_display"<< rc; | ||
78 | continue; | 55 | continue; | ||
79 | } | 56 | } | ||
80 | 57 | | |||
81 | qCDebug(POWERDEVIL) << "adding handle to list"; | 58 | QVector<uint16_t> tmpVcpList; | ||
82 | m_displayHandleList.append(dh); | 59 | for (int iVcp = 0; iVcp < m_usedVcp.count(); ++iVcp) { | ||
83 | qCDebug(POWERDEVIL) << "handles nb: "<<m_displayHandleList.count(); | 60 | DDCA_Any_Vcp_Value *returnValue; | ||
84 | char* capabilityString; | 61 | rc = ddca_get_any_vcp_value(dh, m_usedVcp.value(iVcp), DDCA_NON_TABLE_VCP_VALUE_PARM, &returnValue); | ||
85 | DDCA_Capabilities* parsedCapabilities; | 62 | if (rc < 0) { | ||
86 | ddca_get_capabilities_string(dh, &capabilityString); | 63 | qCDebug(POWERDEVIL) << "[DDCutilBrightness]: This monitor does not seem to support " << m_usedVcp[iVcp]; | ||
broulik: Coding style:
```
for (int iVcp = 0; iVcp < m_usedVcp.count(); ++iVcp) {
``` | |||||
87 | ddca_parse_capabilities_string(capabilityString, &parsedCapabilities); | 64 | } else { | ||
88 | qCInfo(POWERDEVIL) << "[DDCutilBrightness] " << parsedCapabilities->vcp_code_ct << "capabilities parsed"; | 65 | tmpVcpList.append(m_usedVcp.value(iVcp)); | ||
broulik: Use `.value()` instead of `operator[]` which is const | |||||
89 | | ||||
90 | | ||||
91 | m_descrToVcp_perDisp.append(QMap<QString, int>()); | | |||
92 | m_vcpTovcpValueWithDescr_perDisp.append(QMap<int, QMap<int, QString> >() ); | | |||
93 | //fill the feature description to vcp LUT | | |||
94 | | ||||
95 | DDCA_Version_Feature_Info* featureInfo; | | |||
96 | for (int iVcp=0;iVcp<parsedCapabilities->vcp_code_ct;iVcp++) { | | |||
97 | | ||||
98 | int vcpCode=parsedCapabilities->vcp_codes[iVcp].feature_code; | | |||
99 | | ||||
100 | m_vcpTovcpValueWithDescr_perDisp[iDisp].insert(vcpCode, QMap<int, QString>()); | | |||
101 | | ||||
102 | m_descrToVcp_perDisp[iDisp].insert( | | |||
103 | QString(ddca_get_feature_name(vcpCode)), vcpCode); | | |||
104 | | ||||
105 | | ||||
106 | ddca_get_feature_info_by_display(m_displayHandleList.at(iDisp), vcpCode, &featureInfo); | | |||
107 | if (featureInfo == nullptr) { | | |||
108 | continue; | | |||
109 | } | 66 | } | ||
110 | qCDebug(POWERDEVIL) << featureInfo->feature_code<<":"<<featureInfo->desc; | | |||
111 | if ((featureInfo->feature_flags & DDCA_SIMPLE_NC) != DDCA_SIMPLE_NC) { | | |||
112 | continue; | | |||
113 | } | 67 | } | ||
114 | for (int iVcpVal=0;featureInfo->sl_values[iVcpVal].value_code!=0;++iVcpVal) { | 68 | //we only store displays that actually support the features we want. | ||
115 | 69 | if( tmpVcpList.contains(0x10) ) { | |||
broulik: Coding style: Put on same line
```} else {``` | |||||
116 | qCDebug(POWERDEVIL) << "\t"<<featureInfo->sl_values[iVcpVal].value_code | 70 | qCDebug(POWERDEVIL) << "Display supports Brightness, adding handle to list"; | ||
117 | <<":"<< featureInfo->sl_values[iVcpVal].value_name; | 71 | m_displayHandleList.append(dh); | ||
broulik: Remove commented code | |||||
118 | 72 | m_supportedVcp_perDisp.value(iDisp).append(tmpVcpList); | |||
119 | bool thisVcpValIsSupported=false; | | |||
120 | | ||||
121 | for (int iSupportedVcpVal=0; iSupportedVcpVal<parsedCapabilities->vcp_codes[iVcp].value_ct; iSupportedVcpVal++) { | | |||
122 | if(parsedCapabilities->vcp_codes[iVcp].values[iSupportedVcpVal] | | |||
123 | ==featureInfo->sl_values[iVcpVal].value_code) { | | |||
124 | thisVcpValIsSupported=true; | | |||
125 | } | 73 | } | ||
126 | } | 74 | } | ||
127 | 75 | | |||
128 | if (thisVcpValIsSupported) { | 76 | #else | ||
129 | (m_vcpTovcpValueWithDescr_perDisp[iDisp])[vcpCode].insert( | 77 | qCInfo(POWERDEVIL) << "[DDCutilBrightness] compiled without DDC/CI support"; | ||
130 | featureInfo->sl_values[iVcpVal].value_code, | 78 | return; | ||
131 | featureInfo->sl_values[iVcpVal].value_name); | | |||
132 | } | | |||
133 | } | | |||
134 | } | | |||
135 | ddca_free_display_identifier(did); | | |||
136 | ddca_free_parsed_capabilities(parsedCapabilities); | | |||
137 | } | | |||
138 | #endif | 79 | #endif | ||
139 | } | 80 | } | ||
140 | 81 | | |||
141 | 82 | | |||
broulik: Coding style: `if (...) {` | |||||
142 | bool DDCutilBrightness::isSupported() const | 83 | bool DDCutilBrightness::isSupported() const | ||
143 | { | 84 | { | ||
broulik: Leave preprocessor directives unindented | |||||
144 | #ifndef WITH_DDCUTIL | 85 | #ifdef WITH_DDCUTIL | ||
145 | return false; | | |||
146 | #else | | |||
147 | return !m_displayHandleList.isEmpty(); | 86 | return !m_displayHandleList.isEmpty(); | ||
87 | #else | ||||
88 | return false; | ||||
148 | #endif | 89 | #endif | ||
149 | } | 90 | } | ||
150 | 91 | | |||
151 | 92 | | |||
152 | long DDCutilBrightness::brightness() | 93 | long DDCutilBrightness::brightness() | ||
153 | { | 94 | { | ||
154 | #ifdef WITH_DDCUTIL | 95 | #ifdef WITH_DDCUTIL | ||
155 | //we check wether the timer is running, this means we received new values but did not send them yet to the monitor | 96 | //we check wether the timer is running, this means we received new values but did not send them yet to the monitor | ||
156 | //not checking that results in the brightness slider jump to the previous vqlue when changing. | 97 | //not checking that results in the brightness slider jump to the previous value when changing. | ||
157 | if(m_setBrightnessEventFilter.isActive()) { | 98 | if (m_setBrightnessEventFilter.isActive()) { | ||
158 | m_lastBrightnessKnown = m_tmpCurrentBrightness; | 99 | m_lastBrightnessKnown = m_tmpCurrentBrightness; | ||
159 | } | 100 | } else { //FIXME: gets value for display 1 | ||
160 | else { //FIXME: gets value for display 1 | | |||
161 | DDCA_Status rc; | 101 | DDCA_Status rc; | ||
162 | DDCA_Single_Vcp_Value *returnValue; | 102 | DDCA_Any_Vcp_Value *returnValue; | ||
163 | 103 | | |||
164 | rc = ddca_get_vcp_value(m_displayHandleList.at(0), | 104 | rc = ddca_get_any_vcp_value(m_displayHandleList.at(0), | ||
165 | m_descrToVcp_perDisp.at(0).value("Brightness"), | 105 | 0x10, | ||
166 | DDCA_NON_TABLE_VCP_VALUE, &returnValue); | 106 | DDCA_NON_TABLE_VCP_VALUE_PARM, &returnValue); | ||
167 | qCDebug(POWERDEVIL) << "[DDCutilBrightness::brightness]: ddca_get_vcp_value returned" << rc; | 107 | qCDebug(POWERDEVIL) << "[DDCutilBrightness::brightness]: ddca_get_vcp_value returned" << rc; | ||
168 | 108 | | |||
169 | //check rc to prevent crash on wake from idle and the monitor has gone to powersave mode | 109 | //check rc to prevent crash on wake from idle and the monitor has gone to powersave mode | ||
170 | if (rc == 0) { | 110 | if (rc == 0) { | ||
171 | m_lastBrightnessKnown = (long)returnValue->val.c.cur_val; | 111 | m_lastBrightnessKnown = (long)VALREC_CUR_VAL(returnValue); | ||
172 | } | 112 | } | ||
173 | } | 113 | } | ||
174 | return m_lastBrightnessKnown; | 114 | return m_lastBrightnessKnown; | ||
175 | #else | 115 | #else | ||
176 | return 0; | 116 | return 0; | ||
177 | #endif | 117 | #endif | ||
178 | } | 118 | } | ||
179 | 119 | | |||
180 | long DDCutilBrightness::brightnessMax() | 120 | long DDCutilBrightness::brightnessMax() | ||
181 | { | 121 | { | ||
182 | #ifdef WITH_DDCUTIL | 122 | #ifdef WITH_DDCUTIL | ||
183 | DDCA_Status rc; | 123 | DDCA_Status rc; | ||
184 | DDCA_Single_Vcp_Value *returnValue; | 124 | DDCA_Any_Vcp_Value *returnValue; | ||
185 | 125 | | |||
186 | rc = ddca_get_vcp_value(m_displayHandleList.at(0), | 126 | rc = ddca_get_any_vcp_value(m_displayHandleList.at(0), 0x10, | ||
187 | m_descrToVcp_perDisp.at(0).value("Brightness"), | 127 | DDCA_NON_TABLE_VCP_VALUE_PARM, &returnValue); | ||
188 | DDCA_NON_TABLE_VCP_VALUE, &returnValue); | | |||
189 | qCDebug(POWERDEVIL) << "[DDCutilBrightness::brightnessMax]: ddca_get_vcp_value returned" << rc; | 128 | qCDebug(POWERDEVIL) << "[DDCutilBrightness::brightnessMax]: ddca_get_vcp_value returned" << rc; | ||
190 | 129 | | |||
191 | //check rc to prevent crash on wake from idle and the monitor has gone to powersave mode | 130 | //check rc to prevent crash on wake from idle and the monitor has gone to powersave mode | ||
192 | if (rc == 0) { | 131 | if (rc == 0) { | ||
193 | m_lastMaxBrightnessKnown = (long)returnValue->val.c.max_val; | 132 | m_lastMaxBrightnessKnown = (long)VALREC_MAX_VAL(returnValue); | ||
194 | } | 133 | } | ||
195 | 134 | | |||
196 | return m_lastMaxBrightnessKnown; | 135 | return m_lastMaxBrightnessKnown; | ||
197 | #else | 136 | #else | ||
198 | return 100.0; | 137 | return 100.0; | ||
199 | #endif | 138 | #endif | ||
200 | } | 139 | } | ||
201 | 140 | | |||
202 | void DDCutilBrightness::setBrightness(long value) | 141 | void DDCutilBrightness::setBrightness(long value) | ||
203 | { | 142 | { | ||
204 | m_tmpCurrentBrightness = value; | 143 | m_tmpCurrentBrightness = value; | ||
205 | qCDebug(POWERDEVIL) << "[DDCutilBrightness]: saving brightness value: " << value; | 144 | qCDebug(POWERDEVIL) << "[DDCutilBrightness]: saving brightness value: " << value; | ||
206 | m_setBrightnessEventFilter.start(); | 145 | m_setBrightnessEventFilter.start(); | ||
207 | } | 146 | } | ||
208 | 147 | | |||
209 | void DDCutilBrightness::setBrightnessAfterFilter() | 148 | void DDCutilBrightness::setBrightnessAfterFilter() | ||
210 | { | 149 | { | ||
211 | #ifdef WITH_DDCUTIL | 150 | #ifdef WITH_DDCUTIL | ||
212 | DDCA_Status rc; | 151 | DDCA_Status rc; | ||
213 | for (int iDisp=0;iDisp<m_displayHandleList.count();iDisp++) { //FIXME: we set the same brightness to all monitors, plugin architecture needed | 152 | for (int iDisp = 0; iDisp < m_displayHandleList.count(); ++iDisp) { //FIXME: we set the same brightness to all monitors, plugin architecture needed | ||
214 | qCDebug(POWERDEVIL) << "[DDCutilBrightness]: setting brightness "<<m_tmpCurrentBrightness; | 153 | qCDebug(POWERDEVIL) << "[DDCutilBrightness]: setting brightness "<<m_tmpCurrentBrightness; | ||
215 | rc = ddca_set_continuous_vcp_value(m_displayHandleList.at(iDisp), | 154 | rc = ddca_set_continuous_vcp_value(m_displayHandleList.at(iDisp), 0x10, | ||
216 | m_descrToVcp_perDisp.at(iDisp).value("Brightness"), | | |||
217 | (int)m_tmpCurrentBrightness); | 155 | (int)m_tmpCurrentBrightness); | ||
218 | 156 | | |||
219 | if (rc != 0) { | 157 | if (rc < 0) { | ||
220 | qCWarning(POWERDEVIL) << "[DDCutilBrightness::setBrightness] failed, trying to detect()"; | 158 | qCWarning(POWERDEVIL) << "[DDCutilBrightness::setBrightness] failed, trying to detect()"; | ||
221 | detect(); | 159 | detect(); | ||
222 | } else { | 160 | } else { | ||
broulik: Can `m_supportedVcp_perDisp` be empty? `.at(0)` will assert if so. | |||||
223 | m_lastBrightnessKnown = m_tmpCurrentBrightness; | 161 | m_lastBrightnessKnown = m_tmpCurrentBrightness; | ||
224 | } | 162 | } | ||
225 | } | 163 | } | ||
226 | #else | 164 | #else | ||
227 | return; | 165 | return; | ||
228 | #endif | 166 | #endif | ||
229 | } | 167 | } |
Use initializer: