-
Notifications
You must be signed in to change notification settings - Fork 2
/
monitorsettingsdialog.cpp
335 lines (276 loc) · 10.1 KB
/
monitorsettingsdialog.cpp
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 2013 <copyright holder> <email>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "monitorsettingsdialog.h"
#include <QVBoxLayout>
#include <QFrame>
#include <QLabel>
#include <QComboBox>
#include <QProcess>
#include <QGroupBox>
#include <QMessageBox>
#include "ui_monitor.h"
#include <glib.h>
#include <string.h>
struct Monitor {
char* name;
GSList* modeLines;
short currentMode;
short currentRate;
short preferredMode;
short preferredRate;
QCheckBox* enable;
QComboBox* resolutionCombo;
QComboBox* rateCombo;
};
MonitorSettingsDialog::MonitorSettingsDialog():
QDialog(NULL, 0),
monitors(NULL),
LVDS(NULL) {
setupUi();
}
void monitor_free(Monitor* monitor) {
g_free(monitor->name);
g_slist_free(monitor->modeLines);
g_free(monitor);
}
MonitorSettingsDialog::~MonitorSettingsDialog() {
g_slist_foreach(monitors, (GFunc)monitor_free, NULL);
g_slist_free(monitors);
}
QString MonitorSettingsDialog::humanReadableName(Monitor* monitor) {
if(monitor == LVDS)
return tr("Laptop LCD Monitor");
else if(g_str_has_prefix(monitor->name, "VGA") || g_str_has_prefix(monitor->name, "Analog"))
return tr(LVDS ? "External VGA Monitor" : "VGA Monitor");
else if(g_str_has_prefix(monitor->name, "DVI") || g_str_has_prefix(monitor->name, "TMDS") || g_str_has_prefix(monitor->name, "Digital") || g_str_has_prefix(monitor->name, "LVDS"))
return tr(LVDS ? "External DVI Monitor" : "DVI Monitor");
else if(g_str_has_prefix(monitor->name, "TV") || g_str_has_prefix(monitor->name, "S-Video"))
return tr("TV");
else if(strcmp(monitor->name, "default") == 0)
return tr("Default Monitor");
return QString(monitor->name);
}
bool MonitorSettingsDialog::getXRandRInfo() {
// execute xrandr command and read its output
QProcess process;
// set locale to "C" guarantee English output of xrandr
process.processEnvironment().insert("LC_ALL", "c");
process.start("xrandr");
process.waitForFinished(-1);
if(process.exitCode() != 0)
return false;
QByteArray output = process.readAllStandardOutput();
GRegex* regex = g_regex_new("([a-zA-Z]+[-0-9]*) +connected .*((\n +[0-9]+x[0-9]+[^\n]+)+)",
GRegexCompileFlags(0), GRegexMatchFlags(0), NULL);
GMatchInfo* match;
if(g_regex_match(regex, output.constData(), GRegexMatchFlags(0), &match)) {
do {
Monitor* monitor = g_new0(Monitor, 1);
char* modes = g_match_info_fetch(match, 2);
char** lines, **line;
int imode = 0;
monitor->currentMode = monitor->currentRate = -1;
monitor->preferredMode = monitor->preferredRate = -1;
monitor->name = g_match_info_fetch(match, 1);
// check if this is the built-in LCD of laptop
if(! LVDS && (strcmp(monitor->name, "LVDS") == 0 || strcmp(monitor->name, "PANEL") == 0))
LVDS = monitor;
lines = g_strsplit(modes, "\n", -1);
for(line = lines; *line; ++line) {
char* str = strtok(*line, " ");
int irate = 0;
GPtrArray* strv;
if(! str)
continue;
strv = g_ptr_array_sized_new(8);
g_ptr_array_add(strv, g_strdup(str));
while(str = strtok(NULL, " ")) {
if(*str) {
char* star, *plus;
str = g_strdup(str);
// sometimes, + goes after a space
if(0 == strcmp(str, "+"))
--irate;
else
g_ptr_array_add(strv, str);
if(star = strchr(str, '*')) {
monitor->currentMode = imode;
monitor->currentRate = irate;
}
if(plus = strchr(str, '+')) {
monitor->preferredMode = imode;
monitor->preferredRate = irate;
}
if(star)
*star = '\0';
if(plus)
*plus = '\0';
++irate;
}
}
g_ptr_array_add(strv, NULL);
monitor->modeLines = g_slist_append(monitor->modeLines, g_ptr_array_free(strv, FALSE));
++imode;
}
g_strfreev(lines);
g_free(modes);
monitors = g_slist_prepend(monitors, monitor);
}
while(g_match_info_next(match, NULL));
g_match_info_free(match);
}
g_regex_unref(regex);
return true;
}
void MonitorSettingsDialog::onResolutionChanged(int index) {
QComboBox* combo = static_cast<QComboBox*>(sender());
Monitor* monitor = reinterpret_cast<Monitor*>(qVariantValue<void*>(combo->property("monitor")));
char** rate;
int sel = combo->currentIndex();
char** mode_line = reinterpret_cast<char**>(g_slist_nth_data(monitor->modeLines, sel - 1));
monitor->rateCombo->clear();
monitor->rateCombo->addItem(tr("Auto"));
if(sel >= 0 && mode_line && *mode_line) {
for(rate = mode_line + 1; *rate; ++rate)
monitor->rateCombo->addItem(*rate);
}
monitor->rateCombo->setCurrentIndex(0);
}
void MonitorSettingsDialog::setXRandRInfo() {
GSList* l;
QByteArray cmd = "xrandr";
for(l = monitors; l; l = l->next) {
Monitor* monitor = (Monitor*)l->data;
cmd += " --output ";
cmd.append(monitor->name);
cmd.append(' ');
// if the monitor is turned on
if(monitor->enable->isChecked()) {
int sel_res = monitor->resolutionCombo->currentIndex();
int sel_rate = monitor->rateCombo->currentIndex();
if(sel_res < 1) // auto resolution
cmd.append("--auto");
else {
cmd.append("--mode ");
++sel_res; // the fist item in the combo box is "Auto", indecis of resolutions are 1, 2, 3...
cmd.append(monitor->resolutionCombo->currentText());
if(sel_rate >= 1) { // not auto refresh rate
cmd.append(" --rate ");
cmd.append(monitor->resolutionCombo->currentText());
}
}
}
else // turn off
cmd.append("--off");
}
QProcess process;
process.start(cmd);
process.waitForFinished();
}
void MonitorSettingsDialog::chooseMaxResolution(Monitor* monitor) {
if(monitor->resolutionCombo->count() > 1)
monitor->resolutionCombo->setCurrentIndex(1);
}
// turn on both laptop LCD and the external monitor
void MonitorSettingsDialog::onUseBoth() {
for(GSList* l = monitors; l; l = l->next) {
Monitor* monitor = (Monitor*)l->data;
chooseMaxResolution(monitor);
monitor->enable->setChecked(true);
}
accept();
}
// external monitor only
void MonitorSettingsDialog::onExternalOnly() {
for(GSList* l = monitors; l; l = l->next) {
Monitor* monitor = (Monitor*)l->data;
chooseMaxResolution(monitor);
monitor->enable->setChecked(monitor != LVDS);
}
accept();
}
// laptop panel - LVDS only
void MonitorSettingsDialog::onLaptopOnly() {
for(GSList* l = monitors; l; l = l->next) {
Monitor* monitor = (Monitor*)l->data;
chooseMaxResolution(monitor);
monitor->enable->setChecked(monitor == LVDS);
}
accept();
}
void MonitorSettingsDialog::setupUi() {
ui.setupUi(this);
connect(ui.useBoth, SIGNAL(clicked(bool)), SLOT(onUseBoth()));
connect(ui.externalOnly, SIGNAL(clicked(bool)), SLOT(onExternalOnly()));
connect(ui.laptopOnly, SIGNAL(clicked(bool)), SLOT(onLaptopOnly()));
connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(onDialogButtonClicked(QAbstractButton*)));
aboutButton = new QPushButton(ui.buttonBox);
aboutButton->setText(tr("About"));
ui.buttonBox->addButton(aboutButton, QDialogButtonBox::HelpRole);
getXRandRInfo();
// If this is a laptop and there is an external monitor, offer quick options
if(LVDS && g_slist_length(monitors) == 2)
ui.tabWidget->setCurrentIndex(0);
else {
ui.tabWidget->removeTab(0);
}
int i = 0;
GSList* l;
for(l = monitors; l; l = l->next) {
Monitor* monitor = (Monitor*)l->data;
GSList* mode_line;
QGroupBox* box = new QGroupBox(this);
QString title = QString("Monitor %1: %2 (%3)")
.arg(i + 1)
.arg(QString::fromUtf8(monitor->name))
.arg(humanReadableName(monitor));
box->setTitle(title);
Ui::MonitorWidget mui = Ui::MonitorWidget();
mui.setupUi(box);
ui.monitorLayout->insertWidget(ui.monitorLayout->count() - 1, box);
ui.monitorLayout->setStretchFactor(box, 0);
monitor->enable = mui.enabled;
monitor->resolutionCombo = mui.resolution;
monitor->resolutionCombo->setProperty("monitor", qVariantFromValue<void*>(monitor));
monitor->rateCombo = mui.rate;
// turn off screen is not allowed since there should be at least one monitor available.
if(g_slist_length(monitors) == 1)
monitor->enable->setEnabled(false);
if(monitor->currentMode >= 0)
monitor->enable->setChecked(true);
connect(monitor->resolutionCombo, SIGNAL(currentIndexChanged(int)), SLOT(onResolutionChanged(int)));
monitor->resolutionCombo->addItem(tr("Auto"));
for(mode_line = monitor->modeLines; mode_line; mode_line = mode_line->next) {
char** strv = (char**)mode_line->data;
monitor->resolutionCombo->addItem(strv[0]);
}
monitor->resolutionCombo->setCurrentIndex(monitor->currentMode + 1);
monitor->rateCombo->setCurrentIndex(monitor->currentRate + 1);
++i;
}
}
void MonitorSettingsDialog::accept() {
setXRandRInfo();
QDialog::accept();
}
void MonitorSettingsDialog::onDialogButtonClicked(QAbstractButton* button) {
if(ui.buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole)
setXRandRInfo();
else if(button == aboutButton) {
// about dialog
QMessageBox::about(this, tr("About"), tr("LXRandR-Qt\n\nMonitor configuration tool for LXDE."));
}
}