forked from MX-Linux/mx-tweak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brightness_small.cpp
293 lines (262 loc) · 10.2 KB
/
brightness_small.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
#include "QDebug"
#include "QDialog"
#include "QDir"
#include "QKeyEvent"
#include "QMenu"
#include "QScreen"
#include "QSystemTrayIcon"
#include "brightness_small.h"
#include "cmd.h"
#include "ui_brightness_small.h"
brightness_small::brightness_small(QWidget *parent, const QStringList &args) :
QMainWindow(parent),
ui(new Ui::brightness_small)
{
//check to see if running, if so, exit quick
QString check = runCmd(QStringLiteral("ps -aux |grep -E 'mx-tweak.*--tray'|grep -v grep|wc -l")).output;
if ( check.toInt() >= 2) {
qDebug() << "check is " << check;
exit(EXIT_FAILURE);
}
ui->setupUi(this);
setWindowFlags(Qt::CustomizeWindowHint); // for the close, min and max buttons
QIcon save;
save = QIcon::fromTheme(QStringLiteral("gtk-save"));
ui->buttonSave->setIcon(save);
setupDisplay();
QIcon icon;
icon = QIcon::fromTheme(QStringLiteral("brightness-systray"));
setWindowIcon(icon);
setWindowTitle(tr("MX-Tweak"));
expand = false;
QString home_path = QDir::homePath();
QString config_file_path = home_path + "/.config/MX-Linux/MX-Tweak/expand";
if (args.contains(QStringLiteral("--full")) || QFileInfo::exists(config_file_path)) {
expand = true;
}
if (expand) {
ui->label_xbacklight->show();
ui->horizsliderhardwarebacklight->show();
ui->backlight_label->show();
} else {
ui->label_xbacklight->hide();
ui->horizsliderhardwarebacklight->hide();
ui->backlight_label->hide();
}
trayicon = new QSystemTrayIcon;
trayicon->setIcon(icon);
trayicon->show();
this->adjustSize();
menu = new QMenu(this);
if (system("echo $XDG_CURRENT_DESKTOP | grep -q XFCE") == 0) {
full = new QAction(QIcon::fromTheme(QStringLiteral("video-display")), tr("Display"), this);
connect(full, &QAction::triggered, this, &brightness_small::launchfulldisplaydialog);
menu->addAction(full);
menu->addSeparator();
}
quitAction = new QAction(QIcon::fromTheme(QStringLiteral("gtk-quit")), tr("&Quit"), this);
connect(quitAction, &QAction::triggered, qApp, &QGuiApplication::quit);
menu->addAction(quitAction);
connect(trayicon, &QSystemTrayIcon::messageClicked, this, &brightness_small::messageClicked);
connect(trayicon, &QSystemTrayIcon::activated, this, &brightness_small::iconActivated);
trayicon->setContextMenu(menu);
}
void brightness_small::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
setupBrightness();
setupGamma();
setupbacklight();
this->adjustSize();
setPosition();
this->show();
break;
default:
;
}
}
void brightness_small::setPosition()
{
QPoint pos = QCursor::pos();
QScreen *screen = QGuiApplication::screenAt(pos);
if (pos.y() + this->size().height() > screen->availableVirtualGeometry().height())
pos.setY(screen->availableVirtualGeometry().height() - this->size().height());
if (pos.x() + this->size().width() > screen->availableVirtualGeometry().width())
pos.setX(screen->availableVirtualGeometry().width() - this->size().width());
this->move(pos);
}
void brightness_small::messageClicked()
{
this->show();
}
brightness_small::~brightness_small()
{
delete ui;
}
//following function is not actually used by the tray application
void brightness_small::setmissingxfconfvariables(const QString &activeprofile, const QString &resolution)
{
//set resolution, set active, set scales, set display name
//set display name
runCmd("xfconf-query --channel displays -p /" + activeprofile + "/" + ui->comboBoxDisplay->currentText() + " -t string -s " + ui->comboBoxDisplay->currentText() + " --create");
//set resolution
runCmd("xfconf-query --channel displays -p /" + activeprofile + "/" + ui->comboBoxDisplay->currentText() + "/Resolution -t string -s " + resolution.simplified() + " --create");
//set active profile
runCmd("xfconf-query --channel displays -p /" + activeprofile + "/" + ui->comboBoxDisplay->currentText() + "/Active -t bool -s true --create");
}
void brightness_small::setupbacklight()
{
//check for backlights
QString test = runCmd(QStringLiteral("ls /sys/class/backlight")).output;
if ( ! test.isEmpty()) {
//get backlight value for currently
QString backlight=runCmd(QStringLiteral("sudo /usr/lib/mx-tweak/backlight-brightness -g")).output;
int backlight_slider_value = backlight.toInt();
ui->horizsliderhardwarebacklight->setValue(backlight_slider_value);
ui->horizsliderhardwarebacklight->setToolTip(backlight);
ui->backlight_label->setText(backlight);
qDebug() << "backlight string is " << backlight;
qDebug() << " backlight_slider_value is " << backlight_slider_value;
} else {
ui->toolButtonExpandBacklight->hide();
ui->horizsliderhardwarebacklight->hide();
ui->backlight_label->hide();
ui->label_xbacklight->hide();
}
}
void brightness_small::setbacklight()
{
QString backlight = QString::number(ui->horizsliderhardwarebacklight->value());
QString cmd = "sudo /usr/lib/mx-tweak/backlight-brightness -s " + backlight;
ui->backlight_label->setText(backlight);
system(cmd.toUtf8());
}
void brightness_small::setupBrightness()
{
//get brightness value for currently shown display
QString brightness=runCmd("LANG=C xrandr --verbose | awk '/" + ui->comboBoxDisplay->currentText() +"/{flag=1;next}/Clones/{flag=0}flag'|grep Brightness|cut -d' ' -f2").output;
int brightness_slider_value = static_cast<int>(brightness.toFloat() * 100);
ui->horizontalSliderBrightness->setValue(brightness_slider_value);
qDebug() << "brightness string is " << brightness;
qDebug() << " brightness_slider_value is " << brightness_slider_value;
ui->horizontalSliderBrightness->setToolTip(QString::number(ui->horizontalSliderBrightness->value()));
ui->label_brightness_slider->setText(QString::number(ui->horizontalSliderBrightness->value()));
}
void brightness_small::setupGamma()
{
QString gamma = runCmd("/usr/lib/mx-tweak/mx-tweak-lib-randr.sh " + ui->comboBoxDisplay->currentText() + " gamma").output;
gamma=gamma.simplified();
gamma = gamma.section(QStringLiteral(":"),1,3).simplified();
double gamma1 = 1.0 / gamma.section(QStringLiteral(":"),0,0).toDouble();
double gamma2 = 1.0 / gamma.section(QStringLiteral(":"),1,1).toDouble();
double gamma3 = 1.0 / gamma.section(QStringLiteral(":"),2,2).toDouble();
g1 = QString::number(gamma1,'G', 3);
g2 = QString::number(gamma2,'G', 3);
g3 = QString::number(gamma3,'G', 3);
qDebug() << "gamma is " << g1 << " " << g2 << " " << g3;
}
void brightness_small::on_horizontalSliderBrightness_valueChanged(int /*value*/)
{
QString slider_value = QString::number(ui->horizontalSliderBrightness->value());
ui->horizontalSliderBrightness->setToolTip(slider_value);
ui->label_brightness_slider->setText(slider_value);
if ( brightnessflag ) {
//setupBrightness();
//setupGamma();
//setupbacklight();
setBrightness();
}
}
void brightness_small::setBrightness()
{
QString cmd;
double num = ui->horizontalSliderBrightness->value() / 100.0;
qDebug() << "num is :" << num;
QString brightness = QString::number(num, 'G', 5);
qDebug() << "changed brightness is :" << brightness;
cmd = "xrandr --output " + ui->comboBoxDisplay->currentText() + " --brightness " + brightness + " --gamma " + g1 + ":" + g2 + ":" +g3;
system(cmd.toUtf8());
}
void brightness_small::saveBrightness()
{
//save cmd used in user's home file under .config
//make directory when its not present
double num = ui->horizontalSliderBrightness->value() / 100.0;
qDebug() << "num is :" << num;
QString brightness = QString::number(num, 'G', 5);
QString home_path = QDir::homePath();
QString config_file_path = home_path + "/.config/MX-Linux/MX-Tweak/brightness";
if ( ! QFileInfo::exists(config_file_path)) {
runCmd("mkdir -p " + config_file_path);
}
//save config in file named after the display
runCmd("echo 'xrandr --output " + ui->comboBoxDisplay->currentText() + " --brightness " + brightness + " --gamma " + g1 + ":" + g2 + ":" + g3 + "'>" + config_file_path + "/" + ui->comboBoxDisplay->currentText());
}
void brightness_small::on_comboBoxDisplay_currentIndexChanged(int /*index*/)
{
if (brightnessflag) {
setupBrightness();
setupGamma();
}
}
void brightness_small::setupDisplay()
{
//populate combobox
QString displaydata = runCmd(QStringLiteral("LANG=C xrandr |grep -w connected | cut -d' ' -f1")).output;
QStringList displaylist = displaydata.split(QStringLiteral("\n"));
ui->comboBoxDisplay->clear();
ui->comboBoxDisplay->addItems(displaylist);
brightnessflag = true;
}
void brightness_small::on_horizsliderhardwarebacklight_actionTriggered(int /*action*/)
{
setbacklight();
}
// implement change event that closes app when window loses focus
void brightness_small::changeEvent(QEvent *event)
{
QWidget::changeEvent(event);
if (event->type() == QEvent::ActivationChange) {
if (this->isActiveWindow()) {
qDebug() << "focusinEvent";
} else {
qDebug() << "focusOutEvent";
this->hide();
}
}
}
// process keystrokes
void brightness_small::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape) {
this->hide();
}
}
void brightness_small::launchfulldisplaydialog()
{
QString cmd = QStringLiteral("mx-tweak --display");
system(cmd.toUtf8());
}
void brightness_small::on_buttonSave_clicked()
{
saveBrightness();
}
void brightness_small::on_toolButtonExpandBacklight_clicked()
{
QString config_file_path = QDir::homePath() + "/.config/MX-Linux/MX-Tweak/expand";
//expand toggle
expand = !expand;
if (! expand) {
QFile::remove(config_file_path);
ui->label_xbacklight->hide();
ui->horizsliderhardwarebacklight->hide();
ui->backlight_label->hide();
} else {
QFile file(config_file_path);
file.open(QIODevice::NewOnly);
ui->label_xbacklight->show();
ui->horizsliderhardwarebacklight->show();
ui->backlight_label->show();
}
}