1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/* This file is part of the KDE project
* SPDX-FileCopyrightText: 2023 Quang NgĂ´ <ngoquang2708@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.0-only
*
*/
#include "ddcutildisplay.h"
#include <powerdevil_debug.h>
#include <chrono>
#include <span>
using namespace std::chrono_literals;
constexpr std::chrono::milliseconds s_setBrightnessDelay = 1s;
constexpr std::array<std::chrono::milliseconds, 3> s_backoffRetryIntervals = {1s, 2s, 3s};
#ifdef WITH_DDCUTIL
constexpr DDCA_Vcp_Feature_Code BRIGHTNESS_VCP_FEATURE_CODE = 0x10;
DDCutilDisplay::DDCutilDisplay(DDCA_Display_Ref displayRef, QMutex *openDisplayMutex)
: m_displayRef(displayRef)
, m_brightnessWorker(new BrightnessWorker)
, m_timer(new QTimer(this))
, m_retryCounter(0)
, m_openDisplayMutex(openDisplayMutex)
, m_brightness(-1)
, m_maxBrightness(-1)
, m_supportsBrightness(false)
{
Q_ASSERT(m_displayRef != nullptr);
qCDebug(POWERDEVIL) << "[DDCutilDisplay]: Creating display info and handle from display reference...";
DDCA_Status status = DDCRC_OK;
//
// Part 1: display info
DDCA_Display_Info *displayInfo = nullptr;
if (status = ddca_get_display_info(m_displayRef, &displayInfo); status != DDCRC_OK) {
qCWarning(POWERDEVIL) << "[DDCutilDisplay]: ddca_get_display_info" << status;
return;
}
m_label = QString::fromLocal8Bit(displayInfo->model_name);
m_ioPath = displayInfo->path;
m_id = DDCutilDisplay::generatePathId(displayInfo->path);
// the EDID is always guaranteed to be at least 128 bytes long
static_assert(sizeof(DDCA_Display_Info::edid_bytes) == 128);
std::ranges::copy(std::span(displayInfo->edid_bytes, 128), std::back_inserter(m_edidData));
ddca_free_display_info(displayInfo);
// Remaining parts in init(), which can be retried if supportsBrightness() is still false
init();
}
void DDCutilDisplay::init()
{
// Part 2: temporarily opened display handle
//
// We don't want to keep it open permanently, because doing so will block other programs
// backed by libddcutil (like the ddcutil CLI itself) from functioning.
{
// We can't conflict with this object's own brightness setter, but we could conflict with
// the brightness setter of a different, previously created object running simultaneously.
QMutexLocker locker(m_openDisplayMutex);
DDCA_Status status = DDCRC_OK;
DDCA_Display_Handle displayHandle = nullptr;
if (status = ddca_open_display2(m_displayRef, true, &displayHandle); status != DDCRC_OK) {
qCWarning(POWERDEVIL) << "[DDCutilDisplay]: ddca_open_display2" << status;
return;
}
DDCA_Non_Table_Vcp_Value value;
if (status = ddca_get_non_table_vcp_value(displayHandle, BRIGHTNESS_VCP_FEATURE_CODE, &value); status != DDCRC_OK) {
qCWarning(POWERDEVIL) << "[DDCutilDisplay]: ddca_get_non_table_vcp_value" << status;
} else {
m_brightness = value.sh << 8 | value.sl;
m_maxBrightness = value.mh << 8 | value.ml;
m_supportsBrightness = true;
}
if (status = ddca_close_display(displayHandle); status != DDCRC_OK) {
qCWarning(POWERDEVIL) << "[DDCutilDisplay]: ddca_close_display" << status;
}
if (status != DDCRC_OK || !m_supportsBrightness) {
return;
}
}
//
// Part 3: timer & worker setup
m_timer->setSingleShot(true);
disconnect(m_timer, &QTimer::timeout, nullptr, nullptr);
connect(m_timer, &QTimer::timeout, this, &DDCutilDisplay::onSetBrightnessTimeout);
m_brightnessWorker->moveToThread(&m_brightnessWorkerThread);
connect(&m_brightnessWorkerThread, &QThread::finished, m_brightnessWorker, &QObject::deleteLater);
connect(this, &DDCutilDisplay::ddcBrightnessChangeRequested, m_brightnessWorker, &BrightnessWorker::ddcSetBrightness);
connect(m_brightnessWorker, &BrightnessWorker::ddcBrightnessChangeApplied, this, &DDCutilDisplay::ddcBrightnessChangeFinished);
m_brightnessWorkerThread.start();
Q_EMIT supportsBrightnessChanged(true);
}
DDCA_IO_Path DDCutilDisplay::ioPath() const
{
return m_ioPath;
}
QString DDCutilDisplay::generatePathId(const DDCA_IO_Path &displayPath)
{
switch (displayPath.io_mode) {
case DDCA_IO_I2C:
return QStringLiteral("i2c:%1").arg(displayPath.path.i2c_busno);
case DDCA_IO_USB:
return QStringLiteral("usb:%1").arg(displayPath.path.hiddev_devno);
}
return QString();
}
#endif
DDCutilDisplay::~DDCutilDisplay()
{
#ifdef WITH_DDCUTIL
m_brightnessWorkerThread.quit();
m_brightnessWorkerThread.wait();
#endif
}
QString DDCutilDisplay::id() const
{
return m_id;
}
QString DDCutilDisplay::label() const
{
return m_label;
}
int DDCutilDisplay::knownSafeMinBrightness() const
{
// External monitors are not known to turn off completely when their brightness goes to 0.
return 0;
}
int DDCutilDisplay::maxBrightness() const
{
return m_maxBrightness;
}
int DDCutilDisplay::brightness() const
{
return m_brightness;
}
void DDCutilDisplay::scheduleRetryInit()
{
disconnect(m_timer, &QTimer::timeout, nullptr, nullptr);
connect(m_timer, &QTimer::timeout, this, &DDCutilDisplay::onInitRetryTimeout);
m_retryCounter = 0;
m_timer->setSingleShot(true);
m_timer->start(m_supportsBrightness ? 0ms : s_backoffRetryIntervals[m_retryCounter]);
qCWarning(POWERDEVIL) << "[DDCutilDisplay]:" << m_label << "retrying to initialize DDC/CI brightness in" << m_timer->interval()
<< "milliseconds - attempt no." << (m_retryCounter + 1);
}
void DDCutilDisplay::onInitRetryTimeout()
{
#ifdef WITH_DDCUTIL
if (!m_supportsBrightness) {
init();
}
if (!m_supportsBrightness) {
if (++m_retryCounter; m_retryCounter < s_backoffRetryIntervals.size()) {
m_timer->start(s_backoffRetryIntervals[m_retryCounter]);
qCWarning(POWERDEVIL) << "[DDCutilDisplay]:" << m_label << "retrying to initialize DDC/CI brightness in" << m_timer->interval()
<< "milliseconds - attempt no." << (m_retryCounter + 1);
return;
}
}
#endif
qCWarning(POWERDEVIL) << "[DDCutilDisplay]:" << m_label << (m_supportsBrightness ? "succeeded" : "failed") << "to initialize DDC/CI brightness";
Q_EMIT retryInitFinished(m_supportsBrightness);
}
void DDCutilDisplay::setBrightness(int value, bool allowAnimations)
{
#ifdef WITH_DDCUTIL
if (m_supportsBrightness) {
m_retryCounter = 0;
m_timer->start(s_setBrightnessDelay);
m_brightness = value;
}
#endif
}
void DDCutilDisplay::onSetBrightnessTimeout()
{
Q_EMIT ddcBrightnessChangeRequested(m_brightness, this);
}
void DDCutilDisplay::ddcBrightnessChangeFinished(bool success)
{
if (!success) {
if (m_retryCounter < s_backoffRetryIntervals.size()) {
m_timer->start(s_backoffRetryIntervals[m_retryCounter]);
++m_retryCounter;
qCWarning(POWERDEVIL) << "[DDCutilDisplay]:" << m_label << "retrying to set DDC/CI brightness in" << m_timer->interval()
<< "milliseconds - attempt no." << m_retryCounter;
return;
}
qCWarning(POWERDEVIL) << "[DDCutilDisplay]:" << m_label << "failed to set DDC/CI brightness";
m_supportsBrightness = false;
Q_EMIT supportsBrightnessChanged(false);
} else if (m_retryCounter > 0) { // only yell if we also logged the "retrying" message
qCWarning(POWERDEVIL) << "[DDCutilDisplay]:" << m_label << "succeeded to set DDC/CI brightness";
}
}
void BrightnessWorker::ddcSetBrightness(int value, DDCutilDisplay *display)
{
#ifdef WITH_DDCUTIL
qCDebug(POWERDEVIL) << "[DDCutilDisplay]:" << display->m_label << "setting brightness to" << value << "with temporary display handle";
DDCA_Display_Handle displayHandle = nullptr;
DDCA_Status status = DDCRC_OK;
{
QMutexLocker locker(display->m_openDisplayMutex);
if (status = ddca_open_display2(display->m_displayRef, true, &displayHandle); status != DDCRC_OK) {
qCWarning(POWERDEVIL) << "[DDCutilDisplay]: ddca_open_display2" << status;
} else {
int currentBrightness = -1;
DDCA_Non_Table_Vcp_Value vcpValue;
if (status = ddca_get_non_table_vcp_value(displayHandle, BRIGHTNESS_VCP_FEATURE_CODE, &vcpValue); status != DDCRC_OK) {
qCWarning(POWERDEVIL) << "[DDCutilDisplay]: ddca_get_non_table_vcp_value" << status;
} else {
currentBrightness = vcpValue.sh << 8 | vcpValue.sl;
}
if (value == currentBrightness) {
qCDebug(POWERDEVIL) << "[DDCutilDisplay]:" << display->m_label << "hardware brightness already at" << value;
} else {
uint8_t sh = value >> 8 & 0xff;
uint8_t sl = value & 0xff;
if (status = ddca_set_non_table_vcp_value(displayHandle, BRIGHTNESS_VCP_FEATURE_CODE, sh, sl); status != DDCRC_OK) {
qCWarning(POWERDEVIL) << "[DDCutilDisplay]: ddca_set_non_table_vcp_value" << status;
}
}
if (DDCA_Status closeStatus = ddca_close_display(displayHandle); closeStatus != DDCRC_OK) {
qCWarning(POWERDEVIL) << "[DDCutilDisplay]: ddca_close_display" << closeStatus;
status = closeStatus;
}
}
}
Q_EMIT ddcBrightnessChangeApplied(status == DDCRC_OK);
#endif
}
bool DDCutilDisplay::supportsBrightness() const
{
return m_supportsBrightness;
}
std::optional<QByteArray> DDCutilDisplay::edidData() const
{
#ifdef WITH_DDCUTIL
return m_edidData;
#else
return std::nullopt;
#endif
}
bool DDCutilDisplay::usesDdcCi() const
{
return true;
}
#include "moc_ddcutildisplay.cpp"
|