Changeset View
Changeset View
Standalone View
Standalone View
backends/upower/ddcutilbrightness.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2017 Dorian Vogel <dorianvogel@gmail.com> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Library General Public | ||||
6 | * License version 2 as published by the Free Software Foundation. | ||||
7 | * | ||||
8 | * This library is distributed in the hope that it will be useful, | ||||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
11 | * Library General Public License for more details. | ||||
12 | * | ||||
13 | * You should have received a copy of the GNU Library General Public License | ||||
14 | * along with this library; see the file COPYING.LIB. If not, write to | ||||
15 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
16 | * Boston, MA 02110-1301, USA. | ||||
17 | * | ||||
18 | */ | ||||
19 | | ||||
20 | #include <powerdevil_debug.h> | ||||
21 | #include "ddcutilbrightness.h" | ||||
22 | | ||||
23 | DDCutilBrightness::DDCutilBrightness() | ||||
24 | : m_usedVcp({0x10}) | ||||
25 | { | ||||
26 | m_setBrightnessEventFilter.setInterval(100); | ||||
27 | m_setBrightnessEventFilter.setSingleShot(true); | ||||
28 | connect(&m_setBrightnessEventFilter, &QTimer::timeout, this, &DDCutilBrightness::setBrightnessAfterFilter); | ||||
29 | } | ||||
30 | | ||||
31 | void DDCutilBrightness::detect() | ||||
32 | { | ||||
33 | #ifdef WITH_DDCUTIL | ||||
34 | DDCA_Status rc; | ||||
35 | | ||||
36 | qCDebug(POWERDEVIL) << "Check for monitors using ddca_get_displays()..."; | ||||
37 | // Inquire about detected monitors. | ||||
38 | DDCA_Display_Info_List * dlist = nullptr; | ||||
39 | ddca_get_display_info_list2(true, &dlist); | ||||
40 | qCInfo(POWERDEVIL) << "[DDCutilBrightness] " << dlist->ct << "display(s) were detected"; | ||||
41 | | ||||
42 | for (int iDisp = 0; iDisp < dlist->ct; ++iDisp) { | ||||
43 | DDCA_Display_Handle dh = nullptr; // initialize to avoid clang analyzer warning | ||||
44 | | ||||
45 | qCDebug(POWERDEVIL) << "Create a Display Identifier for display"<<iDisp << | ||||
46 | " : "<< dlist->info[iDisp].model_name; | ||||
47 | | ||||
48 | m_displayInfoList.append(dlist->info[iDisp]); | ||||
49 | | ||||
50 | qCDebug(POWERDEVIL) << "Opening the display reference, creating a display handle..."; | ||||
51 | rc = ddca_open_display2(dlist->info[iDisp].dref, true, &dh); | ||||
52 | if (rc != 0) { | ||||
53 | qCWarning(POWERDEVIL) << "[DDCutilBrightness]: ddct_open_display"<< rc; | ||||
54 | continue; | ||||
55 | } | ||||
56 | | ||||
57 | DDCA_Feature_List vcpList; | ||||
58 | ddca_get_feature_list_by_dref(DDCA_SUBSET_COLOR, dh, false, &vcpList); | ||||
59 | qCDebug(POWERDEVIL) << "ddca_feature_list: "<< ddca_feature_list_string(&vcpList, "0x", ","); | ||||
60 | QVector<uint16_t> tmpVcpList; | ||||
61 | for (int iVcp = 0; iVcp < m_usedVcp.count(); ++iVcp) { | ||||
62 | DDCA_Non_Table_Vcp_Value returnValue; | ||||
63 | rc = ddca_get_non_table_vcp_value(dh,m_usedVcp.value(iVcp), &returnValue); | ||||
64 | | ||||
65 | if (rc < 0) { | ||||
66 | qCDebug(POWERDEVIL) << "[DDCutilBrightness]: This monitor does not seem to support " << m_usedVcp[iVcp]; | ||||
67 | } else { | ||||
68 | qCDebug(POWERDEVIL) << "[DDCutilBrightness]: This monitor supports " << m_usedVcp[iVcp]; | ||||
69 | tmpVcpList.append(m_usedVcp.value(iVcp)); | ||||
70 | } | ||||
71 | } | ||||
72 | //we only store displays that actually support the features we want. | ||||
73 | if (tmpVcpList.contains(0x10)) { | ||||
74 | qCDebug(POWERDEVIL) << "Display supports Brightness, adding handle to list"; | ||||
75 | m_displayHandleList.append(dh); | ||||
76 | m_supportedVcp_perDisp.value(iDisp).append(tmpVcpList); | ||||
77 | } | ||||
78 | } | ||||
79 | #else | ||||
80 | qCInfo(POWERDEVIL) << "[DDCutilBrightness] compiled without DDC/CI support"; | ||||
81 | return; | ||||
82 | #endif | ||||
83 | } | ||||
84 | | ||||
85 | bool DDCutilBrightness::isSupported() const | ||||
86 | { | ||||
87 | #ifdef WITH_DDCUTIL | ||||
88 | return !m_displayHandleList.isEmpty(); | ||||
89 | #else | ||||
90 | return false; | ||||
91 | #endif | ||||
92 | } | ||||
93 | | ||||
94 | long DDCutilBrightness::brightness() | ||||
95 | { | ||||
96 | #ifdef WITH_DDCUTIL | ||||
97 | //we check whether the timer is running, this means we received new values but did not send them yet to the monitor | ||||
98 | //not checking that results in the brightness slider jump to the previous value when changing. | ||||
99 | if (m_setBrightnessEventFilter.isActive()) { | ||||
100 | m_lastBrightnessKnown = m_tmpCurrentBrightness; | ||||
101 | } else { //FIXME: gets value for display 1 | ||||
102 | DDCA_Status rc; | ||||
103 | DDCA_Non_Table_Vcp_Value returnValue; | ||||
104 | | ||||
105 | rc = ddca_get_non_table_vcp_value(m_displayHandleList.at(0), | ||||
106 | 0x10, &returnValue); | ||||
107 | qCDebug(POWERDEVIL) << "[DDCutilBrightness::brightness]: ddca_get_vcp_value returned" << rc; | ||||
108 | | ||||
109 | //check rc to prevent crash on wake from idle and the monitor has gone to powersave mode | ||||
110 | if (rc == 0) { | ||||
111 | m_lastBrightnessKnown = (long)(returnValue.sh <<8|returnValue.sl); | ||||
112 | } | ||||
113 | } | ||||
114 | return m_lastBrightnessKnown; | ||||
115 | #else | ||||
116 | return 0; | ||||
117 | #endif | ||||
118 | } | ||||
119 | | ||||
120 | long DDCutilBrightness::brightnessMax() | ||||
121 | { | ||||
122 | #ifdef WITH_DDCUTIL | ||||
123 | DDCA_Status rc; | ||||
124 | DDCA_Non_Table_Vcp_Value returnValue; | ||||
125 | | ||||
126 | rc = ddca_get_non_table_vcp_value(m_displayHandleList.at(0) | ||||
127 | , 0x10, &returnValue); | ||||
128 | qCDebug(POWERDEVIL) << "[DDCutilBrightness::brightnessMax]: ddca_get_vcp_value returned" << rc; | ||||
129 | | ||||
130 | //check rc to prevent crash on wake from idle and the monitor has gone to powersave mode | ||||
131 | if (rc == 0) { | ||||
132 | m_lastMaxBrightnessKnown = (long)(returnValue.mh <<8|returnValue.ml); | ||||
133 | } | ||||
134 | | ||||
135 | return m_lastMaxBrightnessKnown; | ||||
136 | #else | ||||
137 | return 100.0; | ||||
138 | #endif | ||||
139 | } | ||||
140 | | ||||
141 | void DDCutilBrightness::setBrightness(long value) | ||||
142 | { | ||||
143 | m_tmpCurrentBrightness = value; | ||||
144 | qCDebug(POWERDEVIL) << "[DDCutilBrightness]: saving brightness value: " << value; | ||||
145 | m_setBrightnessEventFilter.start(); | ||||
146 | } | ||||
147 | | ||||
148 | void DDCutilBrightness::setBrightnessAfterFilter() | ||||
149 | { | ||||
150 | #ifdef WITH_DDCUTIL | ||||
151 | DDCA_Status rc; | ||||
152 | for (int iDisp = 0; iDisp < m_displayHandleList.count(); ++iDisp) { //FIXME: we set the same brightness to all monitors, plugin architecture needed | ||||
153 | qCDebug(POWERDEVIL) << "[DDCutilBrightness]: setting brightness "<<m_tmpCurrentBrightness; | ||||
154 | uint8_t newsh = (uint16_t)m_tmpCurrentBrightness >> 8; | ||||
155 | uint8_t newsl = (uint16_t)m_tmpCurrentBrightness & 0x00ff; | ||||
156 | rc = ddca_set_non_table_vcp_value(m_displayHandleList.at(iDisp), 0x10, | ||||
157 | newsh, newsl); | ||||
158 | | ||||
159 | if (rc < 0) { | ||||
160 | qCWarning(POWERDEVIL) << "[DDCutilBrightness::setBrightness] failed, trying to detect()"; | ||||
161 | detect(); | ||||
162 | } else { | ||||
163 | m_lastBrightnessKnown = m_tmpCurrentBrightness; | ||||
164 | } | ||||
165 | } | ||||
166 | #else | ||||
167 | return; | ||||
168 | #endif | ||||
169 | } |