-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathgreeter-display-setting.cpp
More file actions
299 lines (261 loc) · 10.7 KB
/
Copy pathgreeter-display-setting.cpp
File metadata and controls
299 lines (261 loc) · 10.7 KB
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
293
294
295
296
297
298
299
// SPDX-FileCopyrightText: 2015 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "constants.h"
#include "dbusconstant.h"
#include <QDBusInterface>
#include <QDBusReply>
#include <QJsonParseError>
#include <QJsonObject>
#include <QDebug>
#include <QFile>
#include <QProcess>
#include <DConfig>
#include <X11/Xlib.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/Xrandr.h>
#include <cmath>
#include <iostream>
DCORE_USE_NAMESPACE
Q_LOGGING_CATEGORY(DDE_SHELL, "org.deepin.dde.shell")
const bool IsWayland = qgetenv("XDG_SESSION_TYPE").contains("wayland");
// Load system cursor --end
bool isScaleConfigExists() {
QDBusInterface configInter(DSS_DBUS::systemDisplayService,
DSS_DBUS::systemDisplayPath,
DSS_DBUS::systemDisplayService,
QDBusConnection::systemBus());
if (!configInter.isValid()) {
return false;
}
QDBusReply<QString> configReply = configInter.call("GetConfig");
if (configReply.isValid()) {
QString config = configReply.value();
QJsonParseError jsonError;
QJsonDocument jsonDoc(QJsonDocument::fromJson(config.toStdString().data(), &jsonError));
if (jsonError.error == QJsonParseError::NoError) {
QJsonObject rootObj = jsonDoc.object();
QJsonObject Config = rootObj.value("Config").toObject();
return !Config.value("ScaleFactors").toObject().isEmpty();
} else {
return false;
}
} else {
qCWarning(DDE_SHELL) << "Call `GetConfig` failed: " << configReply.error().message();
return false;
}
}
// 参照后端算法,保持一致
float toListedScaleFactor(float s) {
const float min = 1.0, max = 3.0, step = 0.25;
if (s <= min) {
return min;
} else if (s >= max) {
return max;
}
for (float i = min; i <= max; i += step) {
if (i > s) {
float ii = i - step;
float d1 = s - ii;
float d2 = i - s;
if (d1 >= d2) {
return i;
} else {
return ii;
}
}
}
return max;
}
static double calcMaxScaleFactor(unsigned int width, unsigned int height) {
const QList<double> scaleFactors = {1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0};
// 如果是竖屏,则反置width, height,防止计算的缩放有误
if (width < height) {
std::swap(width, height);
}
double maxWScale = width / 1024.0;
double maxHScale = height / 768.0;
double maxValue = 0.0;
if (maxWScale < maxHScale) {
maxValue = maxWScale;
} else {
maxValue = maxHScale;
}
if (maxValue > 3.0) {
maxValue = 3.0;
}
double maxScale = 1.0;
for (int idx = 0; (idx*0.25 + 1.0) <= maxValue; idx++) {
maxScale = scaleFactors[idx];
}
return maxScale;
}
static QStringList getScaleList()
{
qDebug() << "Get scale list";
Display *display = XOpenDisplay(nullptr);
if (!display) {
qWarning() << "Display is null";
return QStringList{};
}
XRRScreenResources *resources = XRRGetScreenResourcesCurrent(display, DefaultRootWindow(display));
if (!resources) {
resources = XRRGetScreenResources(display, DefaultRootWindow(display));
qCWarning(DDE_SHELL) << "Get current XRR screen resources failed, instead of getting XRR screen resources, resources: " << resources;
}
static const float MinScreenWidth = 1024.0f;
static const float MinScreenHeight = 768.0f;
static const QStringList tvstring = {"1.0", "1.25", "1.5", "1.75", "2.0", "2.25", "2.5", "2.75", "3.0"};
QStringList fscaleList;
if (resources) {
for (int i = 0; i < resources->noutput; i++) {
XRROutputInfo* outputInfo = XRRGetOutputInfo(display, resources, resources->outputs[i]);
if (outputInfo->crtc == 0 || outputInfo->mm_width == 0)
continue;
XRRCrtcInfo *crtInfo = XRRGetCrtcInfo(display, resources, outputInfo->crtc);
if (crtInfo == nullptr)
continue;
auto maxWScale = crtInfo->width / MinScreenWidth;
auto maxHScale = crtInfo->height / MinScreenHeight;
auto maxScale = maxWScale < maxHScale ? maxWScale : maxHScale;
maxScale = maxScale < 3.0f ? maxScale : 3.0f;
if (fscaleList.isEmpty()) {
for (int idx = 0; idx * 0.25f + 1.0f <= maxScale; ++idx) {
fscaleList << tvstring[idx];
}
qDebug() << "First screen scale list:" << fscaleList;
} else {
QStringList tmpList;
for (int idx = 0; idx * 0.25f + 1.0f <= maxScale; ++idx) {
tmpList << tvstring[idx];
}
qDebug() << "Current screen scale list:" << tmpList;
// fscaleList、tmpList两者取交集
for (const auto &scale : fscaleList) {
if (!tmpList.contains(scale))
fscaleList.removeAll(scale);
}
}
}
} else {
qCWarning(DDE_SHELL) << "Get scale factor failed, please check X11 Extension";
}
return fscaleList;
}
static double getScaleFactor() {
Display *display = XOpenDisplay(nullptr);
double scaleFactor = 0.0;
if (!display) {
return scaleFactor;
}
XRRScreenResources *resources = XRRGetScreenResourcesCurrent(display, DefaultRootWindow(display));
if (!resources) {
resources = XRRGetScreenResources(display, DefaultRootWindow(display));
qCWarning(DDE_SHELL) << "Get current XRR screen resources failed, instead of getting XRR screen resources, resources: " << resources;
}
if (resources) {
double minScaleFactor = 3.0;
for (int i = 0; i < resources->noutput; i++) {
XRROutputInfo* outputInfo = XRRGetOutputInfo(display, resources, resources->outputs[i]);
if (outputInfo->crtc == 0 || outputInfo->mm_width == 0) continue;
XRRCrtcInfo *crtInfo = XRRGetCrtcInfo(display, resources, outputInfo->crtc);
if (crtInfo == nullptr) continue;
// 参照后端的算法,与后端保持一致
float lenPx = hypot(static_cast<double>(crtInfo->width), static_cast<double>(crtInfo->height));
float lenMm = hypot(static_cast<double>(outputInfo->mm_width), static_cast<double>(outputInfo->mm_height));
float lenPxStd = hypot(1920, 1080);
float lenMmStd = hypot(477, 268);
float fix = (lenMm - lenMmStd) * (lenPx / lenPxStd) * 0.00158;
if (fix > 0.5) {
fix = 0.5;
}
if (fix < -0.5) {
fix = -0.5;
}
float scale = (lenPx / lenMm) / (lenPxStd / lenMmStd) + fix;
scaleFactor = toListedScaleFactor(scale);
double maxScaleFactor = calcMaxScaleFactor(crtInfo->width, crtInfo->height);
if (scaleFactor > maxScaleFactor) {
scaleFactor = maxScaleFactor;
}
if (minScaleFactor > scaleFactor) {
minScaleFactor = scaleFactor;
}
}
scaleFactor = scaleFactor > 0.0 ? minScaleFactor : 1;
}
else {
qCWarning(DDE_SHELL) << "Get scale factor failed, please check X11 Extension";
}
return scaleFactor;
}
// 读取系统配置文件的缩放比,如果是null,默认返回1
double getScaleFormConfig()
{
double defaultScaleFactor = 1.0;
DTK_CORE_NAMESPACE::DConfig * dconfig = DConfig::create("org.deepin.dde.lightdm-deepin-greeter", "org.deepin.dde.lightdm-deepin-greeter", QString(), nullptr);
//华为机型,从override配置中获取默认缩放比
if (dconfig) {
defaultScaleFactor = dconfig->value("defaultScaleFactors", 1.0).toDouble() ;
qDebug() <<"Default scale factor: " << defaultScaleFactor;
if(defaultScaleFactor < 1.0){
defaultScaleFactor = 1.0;
}
delete dconfig;
dconfig = nullptr;
}
QDBusInterface configInter(DSS_DBUS::systemDisplayService,
DSS_DBUS::systemDisplayPath,
DSS_DBUS::systemDisplayService,
QDBusConnection::systemBus());
if (!configInter.isValid()) {
return defaultScaleFactor;
}
QDBusReply<QString> configReply = configInter.call("GetConfig");
if (configReply.isValid()) {
QString config = configReply.value();
QJsonParseError jsonError;
QJsonDocument jsonDoc(QJsonDocument::fromJson(config.toLatin1(), &jsonError));
if (jsonError.error == QJsonParseError::NoError) {
QJsonObject rootObj = jsonDoc.object();
QJsonObject Config = rootObj.value("Config").toObject();
double scaleFactor = Config.value("ScaleFactors").toObject().value("ALL").toDouble();
qDebug() << "Scale factor from system display config: " << scaleFactor;
if(scaleFactor == 0.0) {
scaleFactor = defaultScaleFactor;
} else {
// 处理关机期间从高分屏换到低分屏的场景
const auto &scales = getScaleList();
qDebug() << "Scales:" << scales;
if (!scales.isEmpty() && !scales.contains(QString::number(scaleFactor, 'f', 2).replace("00", "0"))) {
qInfo() << "Scale factor is not in scales, use default scale factor";
scaleFactor = defaultScaleFactor;
}
}
return scaleFactor;
} else {
return defaultScaleFactor;
}
} else {
qCWarning(DDE_SHELL) << "DBus call `GetConfig` failed, reply is invaild, error: " << configReply.error().message();
return defaultScaleFactor;
}
}
static void setQtScaleFactorEnv() {
const double scaleFactor = IsWayland ? getScaleFormConfig() : getScaleFactor();
qDebug() << "Final scale factor: " << scaleFactor;
if (scaleFactor > 0.0) {
std::cout << QString("QT_SCALE_FACTOR="+QByteArray::number(scaleFactor)).toStdString().c_str() << std::endl;
} else {
std::cout << QString("QT_AUTO_SCREEN_SCALE_FACTOR=1").toStdString().c_str() << std::endl;
}
}
int main(int argc, char* argv[])
{
// x11下,dxcb设置Qt::AA_EnableHighDpiScaling属性后,平台插件会处理缩放,不需要再主动设置QT_SCALE_FACTOR,否则会导致缩放系数相乘,得出错误的qApp->devicePixelRatio()
// wayland下,dwayland平台插件不会处理缩放,需要手动设置QT_SCALE_FACTOR
if (IsWayland) {
setQtScaleFactorEnv();
}
return 0;
}